Mailing List Archive

python/dist/src/Objects fileobject.c,2.160,2.161 stringobject.c,2.160,2.161 unicodeobject.c,2.146,2.147
Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv7685/python/Objects

Modified Files:
fileobject.c stringobject.c unicodeobject.c
Log Message:
Repair widespread misuse of _PyString_Resize. Since it's clear people
don't understand how this function works, also beefed up the docs. The
most common usage error is of this form (often spread out across gotos):

if (_PyString_Resize(&s, n) < 0) {
Py_DECREF(s);
s = NULL;
goto outtahere;
}

The error is that if _PyString_Resize runs out of memory, it automatically
decrefs the input string object s (which also deallocates it, since its
refcount must be 1 upon entry), and sets s to NULL. So if the "if"
branch ever triggers, it's an error to call Py_DECREF(s): s is already
NULL! A correct way to write the above is the simpler (and intended)

if (_PyString_Resize(&s, n) < 0)
goto outtahere;

Bugfix candidate.


Index: fileobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/fileobject.c,v
retrieving revision 2.160
retrieving revision 2.161
diff -C2 -d -r2.160 -r2.161
*** fileobject.c 21 Apr 2002 18:15:20 -0000 2.160
--- fileobject.c 27 Apr 2002 18:44:32 -0000 2.161
***************
*** 1318,1324 ****
}
cleanup:
! if (big_buffer) {
! Py_DECREF(big_buffer);
! }
return list;
}
--- 1318,1322 ----
}
cleanup:
! Py_XDECREF(big_buffer);
return list;
}

Index: stringobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/stringobject.c,v
retrieving revision 2.160
retrieving revision 2.161
diff -C2 -d -r2.160 -r2.161
*** stringobject.c 22 Apr 2002 17:42:37 -0000 2.160
--- stringobject.c 27 Apr 2002 18:44:32 -0000 2.161
***************
*** 1870,1875 ****
}
/* Fix the size of the resulting string */
! if (inlen > 0 &&_PyString_Resize(&result, output-output_start))
! return NULL;
return result;
}
--- 1870,1875 ----
}
/* Fix the size of the resulting string */
! if (inlen > 0)
! _PyString_Resize(&result, output - output_start);
return result;
}
***************
*** 2928,2932 ****
as creating a new string object and destroying the old one, only
more efficiently. In any case, don't use this if the string may
! already be known to some other part of the code... */

int
--- 2928,2939 ----
as creating a new string object and destroying the old one, only
more efficiently. In any case, don't use this if the string may
! already be known to some other part of the code...
! Note that if there's not enough memory to resize the string, the original
! string object at *pv is deallocated, *pv is set to NULL, an "out of
! memory" exception is set, and -1 is returned. Else (on success) 0 is
! returned, and the value in *pv may or may not be the same as on input.
! As always, an extra byte is allocated for a trailing \0 byte (newsize
! does *not* include that), and a trailing \0 byte is stored.
! */

int

Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.146
retrieving revision 2.147
diff -C2 -d -r2.146 -r2.147
*** unicodeobject.c 27 Apr 2002 18:03:26 -0000 2.146
--- unicodeobject.c 27 Apr 2002 18:44:32 -0000 2.147
***************
*** 928,935 ****
}

! if (_PyString_Resize(&v, out - start)) {
! Py_DECREF(v);
! return NULL;
! }
return v;
}
--- 928,932 ----
}

! _PyString_Resize(&v, out - start);
return v;
}
***************
*** 1765,1769 ****
if (offset + 12 > PyString_GET_SIZE(repr)) {
if (_PyString_Resize(&repr, PyString_GET_SIZE(repr) + 100))
! goto onError;
p = PyString_AS_STRING(repr) + offset;
}
--- 1762,1766 ----
if (offset + 12 > PyString_GET_SIZE(repr)) {
if (_PyString_Resize(&repr, PyString_GET_SIZE(repr) + 100))
! return NULL;
p = PyString_AS_STRING(repr) + offset;
}
***************
*** 1848,1859 ****

*p = '\0';
! if (_PyString_Resize(&repr, p - PyString_AS_STRING(repr)))
! goto onError;
!
return repr;
-
- onError:
- Py_DECREF(repr);
- return NULL;
}

--- 1845,1850 ----

*p = '\0';
! _PyString_Resize(&repr, p - PyString_AS_STRING(repr));
return repr;
}

***************
*** 1986,1997 ****
}
*p = '\0';
! if (_PyString_Resize(&repr, p - q))
! goto onError;
!
return repr;
-
- onError:
- Py_DECREF(repr);
- return NULL;
}

--- 1977,1982 ----
}
*p = '\0';
! _PyString_Resize(&repr, p - q);
return repr;
}

***************
*** 2093,2098 ****
/* Resize if error handling skipped some characters */
if (s - start < PyString_GET_SIZE(repr))
! if (_PyString_Resize(&repr, s - start))
! goto onError;
return repr;

--- 2078,2082 ----
/* Resize if error handling skipped some characters */
if (s - start < PyString_GET_SIZE(repr))
! _PyString_Resize(&repr, s - start);
return repr;

***************
*** 2241,2246 ****
/* Resize if error handling skipped some characters */
if (s - start < PyString_GET_SIZE(repr))
! if (_PyString_Resize(&repr, s - start))
! goto onError;
return repr;

--- 2225,2229 ----
/* Resize if error handling skipped some characters */
if (s - start < PyString_GET_SIZE(repr))
! _PyString_Resize(&repr, s - start);
return repr;

***************
*** 2589,2598 ****
}
if (s - PyString_AS_STRING(v) < PyString_GET_SIZE(v))
! if (_PyString_Resize(&v, (int)(s - PyString_AS_STRING(v))))
! goto onError;
return v;

onError:
! Py_DECREF(v);
return NULL;
}
--- 2572,2580 ----
}
if (s - PyString_AS_STRING(v) < PyString_GET_SIZE(v))
! _PyString_Resize(&v, (int)(s - PyString_AS_STRING(v)));
return v;

onError:
! Py_XDECREF(v);
return NULL;
}