Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 572d2d9

Browse files
committed
Fix memory leaks in join & joinfields
1 parent e77a757 commit 572d2d9

1 file changed

Lines changed: 25 additions & 11 deletions

File tree

Modules/stropmodule.c

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ strop_split(self, args)
3737
object *self; /* Not used */
3838
object *args;
3939
{
40-
int len, i, j;
40+
int len, i, j, err;
4141
char *s;
4242
char c;
4343
object *list, *item;
@@ -61,7 +61,13 @@ strop_split(self, args)
6161
}
6262
if (j < i) {
6363
item = newsizedstringobject(s+j, (int)(i-j));
64-
if (item == NULL || addlistitem(list, item) < 0) {
64+
if (item == NULL) {
65+
DECREF(list);
66+
return NULL;
67+
}
68+
err = addlistitem(list, item);
69+
DECREF(item);
70+
if (err < 0) {
6571
DECREF(list);
6672
return NULL;
6773
}
@@ -77,7 +83,7 @@ strop_splitfields(self, args)
7783
object *self; /* Not used */
7884
object *args;
7985
{
80-
int len, n, i, j;
86+
int len, n, i, j, err;
8187
char *s, *sub;
8288
object *list, *item;
8389

@@ -96,22 +102,30 @@ strop_splitfields(self, args)
96102
while (i+n <= len) {
97103
if (s[i] == sub[0] && (n == 1 || strncmp(s+i, sub, n) == 0)) {
98104
item = newsizedstringobject(s+j, (int)(i-j));
99-
if (item == NULL || addlistitem(list, item) < 0) {
100-
DECREF(list);
101-
return NULL;
102-
}
105+
if (item == NULL)
106+
goto fail;
107+
err = addlistitem(list, item);
108+
DECREF(item);
109+
if (err < 0)
110+
goto fail;
103111
i = j = i + n;
104112
}
105113
else
106114
i++;
107115
}
108116
item = newsizedstringobject(s+j, (int)(len-j));
109-
if (item == NULL || addlistitem(list, item) < 0) {
110-
DECREF(list);
111-
return NULL;
112-
}
117+
if (item == NULL)
118+
goto fail;
119+
err = addlistitem(list, item);
120+
DECREF(item);
121+
if (err < 0)
122+
goto fail;
113123

114124
return list;
125+
126+
fail:
127+
DECREF(list);
128+
return NULL;
115129
}
116130

117131

0 commit comments

Comments
 (0)