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

Skip to content

Commit bf32583

Browse files
committed
string_join(): Fix memory leaks discovered by Charles Waldman (and a
few other paths through the function that leaked).
1 parent 7f3cfd5 commit bf32583

1 file changed

Lines changed: 15 additions & 5 deletions

File tree

Objects/stringobject.c

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -709,8 +709,10 @@ string_join(self, args)
709709
goto finally;
710710
slen = PyString_GET_SIZE(sitem);
711711
while (reslen + slen + seplen >= sz) {
712-
if (_PyString_Resize(&res, sz*2))
712+
if (_PyString_Resize(&res, sz*2)) {
713+
Py_DECREF(sitem);
713714
goto finally;
715+
}
714716
sz *= 2;
715717
p = PyString_AsString(res) + reslen;
716718
}
@@ -720,6 +722,7 @@ string_join(self, args)
720722
reslen += seplen;
721723
}
722724
memcpy(p, PyString_AS_STRING(sitem), slen);
725+
Py_DECREF(sitem);
723726
p += slen;
724727
reslen += slen;
725728
}
@@ -728,14 +731,20 @@ string_join(self, args)
728731
for (i = 0; i < seqlen; i++) {
729732
PyObject *item = PySequence_GetItem(seq, i);
730733
PyObject *sitem;
731-
if (!item || !(sitem = PyObject_Str(item))) {
732-
Py_XDECREF(item);
734+
735+
if (!item)
733736
goto finally;
734-
}
737+
sitem = PyObject_Str(item);
738+
Py_DECREF(item);
739+
if (!sitem)
740+
goto finally;
741+
735742
slen = PyString_GET_SIZE(sitem);
736743
while (reslen + slen + seplen >= sz) {
737-
if (_PyString_Resize(&res, sz*2))
744+
if (_PyString_Resize(&res, sz*2)) {
745+
Py_DECREF(sitem);
738746
goto finally;
747+
}
739748
sz *= 2;
740749
p = PyString_AsString(res) + reslen;
741750
}
@@ -745,6 +754,7 @@ string_join(self, args)
745754
reslen += seplen;
746755
}
747756
memcpy(p, PyString_AS_STRING(sitem), slen);
757+
Py_DECREF(sitem);
748758
p += slen;
749759
reslen += slen;
750760
}

0 commit comments

Comments
 (0)