Fix for reference leak in current Python CVS

Barry Warsaw bwarsaw at python.org
Sat Mar 4 21:12:36 EST 2000


>>>>> "CGW" == Charles G Waldman <cgw at fnal.gov> writes:

    CGW> Apparently, somebody added code to `string_join' to do string
    CGW> coercion on non-string sequence elements (a nice addition),
    CGW> but forgot to decref the resulting values returned from
    CGW> PyObject_Str.

That someone was probably me :)

Anyway, I think you're right about the leaks, but your patch doesn't
quite catch them all.  Try this one.

-Barry

P.S. patches at python.org is now the best official place to send patches.

-------------------- snip snip --------------------
Index: stringobject.c
===================================================================
RCS file: /projects/cvsroot/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.55
diff -c -r2.55 stringobject.c
*** stringobject.c	2000/02/29 13:59:28	2.55
--- stringobject.c	2000/03/05 02:03:59
***************
*** 709,716 ****
  				goto finally;
  			slen = PyString_GET_SIZE(sitem);
  			while (reslen + slen + seplen >= sz) {
! 				if (_PyString_Resize(&res, sz*2))
  					goto finally;
  				sz *= 2;
  				p = PyString_AsString(res) + reslen;
  			}
--- 709,718 ----
  				goto finally;
  			slen = PyString_GET_SIZE(sitem);
  			while (reslen + slen + seplen >= sz) {
! 				if (_PyString_Resize(&res, sz*2)) {
! 					Py_DECREF(sitem);
  					goto finally;
+ 				}
  				sz *= 2;
  				p = PyString_AsString(res) + reslen;
  			}
***************
*** 720,725 ****
--- 722,728 ----
  				reslen += seplen;
  			}
  			memcpy(p, PyString_AS_STRING(sitem), slen);
+ 			Py_DECREF(sitem);
  			p += slen;
  			reslen += slen;
  		}
***************
*** 728,741 ****
  		for (i = 0; i < seqlen; i++) {
  			PyObject *item = PySequence_GetItem(seq, i);
  			PyObject *sitem;
! 			if (!item || !(sitem = PyObject_Str(item))) {
! 				Py_XDECREF(item);
  				goto finally;
! 			}
  			slen = PyString_GET_SIZE(sitem);
  			while (reslen + slen + seplen >= sz) {
! 				if (_PyString_Resize(&res, sz*2))
  					goto finally;
  				sz *= 2;
  				p = PyString_AsString(res) + reslen;
  			}
--- 731,750 ----
  		for (i = 0; i < seqlen; i++) {
  			PyObject *item = PySequence_GetItem(seq, i);
  			PyObject *sitem;
! 
! 			if (!item)
  				goto finally;
! 			sitem = PyObject_Str(item);
! 			Py_DECREF(item);
! 			if (!sitem)
! 				goto finally;
! 
  			slen = PyString_GET_SIZE(sitem);
  			while (reslen + slen + seplen >= sz) {
! 				if (_PyString_Resize(&res, sz*2)) {
! 					Py_DECREF(sitem);
  					goto finally;
+ 				}
  				sz *= 2;
  				p = PyString_AsString(res) + reslen;
  			}
***************
*** 745,750 ****
--- 754,760 ----
  				reslen += seplen;
  			}
  			memcpy(p, PyString_AS_STRING(sitem), slen);
+ 			Py_DECREF(sitem);
  			p += slen;
  			reslen += slen;
  		}




More information about the Python-list mailing list