comparison Objects/stringobject.c @ 23311:ba6721ae4b80 2.1

backport tim_one's patch: [Re-did unicodeobject.c - it's changed a lot since 2.1 :) Pretty confident that it's correct] 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. Original patch(es): python/dist/src/Objects/fileobject.c:2.161 python/dist/src/Objects/stringobject.c:2.161 python/dist/src/Objects/unicodeobject.c:2.147
author Anthony Baxter <anthonybaxter@gmail.com>
date Tue, 30 Apr 2002 03:58:47 +0000
parents a429da7db706
children
comparison
equal deleted inserted replaced
23290:5dd52a9b0438 23311:ba6721ae4b80
1447 Py_DECREF(result); 1447 Py_DECREF(result);
1448 Py_INCREF(input_obj); 1448 Py_INCREF(input_obj);
1449 return input_obj; 1449 return input_obj;
1450 } 1450 }
1451 /* Fix the size of the resulting string */ 1451 /* Fix the size of the resulting string */
1452 if (inlen > 0 &&_PyString_Resize(&result, output-output_start)) 1452 if (inlen > 0)
1453 return NULL; 1453 _PyString_Resize(&result, output - output_start);
1454 return result; 1454 return result;
1455 } 1455 }
1456 1456
1457 1457
1458 /* What follows is used for implementing replace(). Perry Stoll. */ 1458 /* What follows is used for implementing replace(). Perry Stoll. */
2423 /* The following function breaks the notion that strings are immutable: 2423 /* The following function breaks the notion that strings are immutable:
2424 it changes the size of a string. We get away with this only if there 2424 it changes the size of a string. We get away with this only if there
2425 is only one module referencing the object. You can also think of it 2425 is only one module referencing the object. You can also think of it
2426 as creating a new string object and destroying the old one, only 2426 as creating a new string object and destroying the old one, only
2427 more efficiently. In any case, don't use this if the string may 2427 more efficiently. In any case, don't use this if the string may
2428 already be known to some other part of the code... */ 2428 already be known to some other part of the code...
2429 Note that if there's not enough memory to resize the string, the original
2430 string object at *pv is deallocated, *pv is set to NULL, an "out of
2431 memory" exception is set, and -1 is returned. Else (on success) 0 is
2432 returned, and the value in *pv may or may not be the same as on input.
2433 As always, an extra byte is allocated for a trailing \0 byte (newsize
2434 does *not* include that), and a trailing \0 byte is stored.
2435 */
2429 2436
2430 int 2437 int
2431 _PyString_Resize(PyObject **pv, int newsize) 2438 _PyString_Resize(PyObject **pv, int newsize)
2432 { 2439 {
2433 register PyObject *v; 2440 register PyObject *v;