Fix memory leaks in join & joinfields

This commit is contained in:
Guido van Rossum 1993-11-05 10:14:49 +00:00
parent e77a757094
commit 572d2d994d
1 changed files with 25 additions and 11 deletions

View File

@ -37,7 +37,7 @@ strop_split(self, args)
object *self; /* Not used */ object *self; /* Not used */
object *args; object *args;
{ {
int len, i, j; int len, i, j, err;
char *s; char *s;
char c; char c;
object *list, *item; object *list, *item;
@ -61,7 +61,13 @@ strop_split(self, args)
} }
if (j < i) { if (j < i) {
item = newsizedstringobject(s+j, (int)(i-j)); item = newsizedstringobject(s+j, (int)(i-j));
if (item == NULL || addlistitem(list, item) < 0) { if (item == NULL) {
DECREF(list);
return NULL;
}
err = addlistitem(list, item);
DECREF(item);
if (err < 0) {
DECREF(list); DECREF(list);
return NULL; return NULL;
} }
@ -77,7 +83,7 @@ strop_splitfields(self, args)
object *self; /* Not used */ object *self; /* Not used */
object *args; object *args;
{ {
int len, n, i, j; int len, n, i, j, err;
char *s, *sub; char *s, *sub;
object *list, *item; object *list, *item;
@ -96,22 +102,30 @@ strop_splitfields(self, args)
while (i+n <= len) { while (i+n <= len) {
if (s[i] == sub[0] && (n == 1 || strncmp(s+i, sub, n) == 0)) { if (s[i] == sub[0] && (n == 1 || strncmp(s+i, sub, n) == 0)) {
item = newsizedstringobject(s+j, (int)(i-j)); item = newsizedstringobject(s+j, (int)(i-j));
if (item == NULL || addlistitem(list, item) < 0) { if (item == NULL)
DECREF(list); goto fail;
return NULL; err = addlistitem(list, item);
} DECREF(item);
if (err < 0)
goto fail;
i = j = i + n; i = j = i + n;
} }
else else
i++; i++;
} }
item = newsizedstringobject(s+j, (int)(len-j)); item = newsizedstringobject(s+j, (int)(len-j));
if (item == NULL || addlistitem(list, item) < 0) { if (item == NULL)
DECREF(list); goto fail;
return NULL; err = addlistitem(list, item);
} DECREF(item);
if (err < 0)
goto fail;
return list; return list;
fail:
DECREF(list);
return NULL;
} }