[Python-checkins] python/dist/src/Objects unicodeobject.c, 2.200,
2.201
doerwalter at users.sourceforge.net
doerwalter at users.sourceforge.net
Fri Oct 24 10:25:30 EDT 2003
Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1:/tmp/cvs-serv21163/Objects
Modified Files:
unicodeobject.c
Log Message:
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
charmaptranslate_makespace() allocated more memory than required for the
next replacement but didn't remember that fact, so memory size was growing
exponentially every time a replacement string is longer that one character.
This fixes SF bug #828737.
Index: unicodeobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/unicodeobject.c,v
retrieving revision 2.200
retrieving revision 2.201
diff -C2 -d -r2.200 -r2.201
*** unicodeobject.c 18 Oct 2003 09:55:08 -0000 2.200
--- unicodeobject.c 24 Oct 2003 14:25:28 -0000 2.201
***************
*** 3223,3239 ****
Return 0 on success, -1 on error */
static
! int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp, int *outsize,
int requiredsize)
{
! if (requiredsize > *outsize) {
/* remember old output position */
int outpos = *outp-PyUnicode_AS_UNICODE(*outobj);
/* exponentially overallocate to minimize reallocations */
! if (requiredsize < 2 * *outsize)
! requiredsize = 2 * *outsize;
if (_PyUnicode_Resize(outobj, requiredsize) < 0)
return -1;
*outp = PyUnicode_AS_UNICODE(*outobj) + outpos;
- *outsize = requiredsize;
}
return 0;
--- 3223,3239 ----
Return 0 on success, -1 on error */
static
! int charmaptranslate_makespace(PyObject **outobj, Py_UNICODE **outp,
int requiredsize)
{
! int oldsize = PyUnicode_GET_SIZE(*outobj);
! if (requiredsize > oldsize) {
/* remember old output position */
int outpos = *outp-PyUnicode_AS_UNICODE(*outobj);
/* exponentially overallocate to minimize reallocations */
! if (requiredsize < 2 * oldsize)
! requiredsize = 2 * oldsize;
if (_PyUnicode_Resize(outobj, requiredsize) < 0)
return -1;
*outp = PyUnicode_AS_UNICODE(*outobj) + outpos;
}
return 0;
***************
*** 3246,3257 ****
Return 0 on success, -1 on error. */
static
! int charmaptranslate_output(Py_UNICODE c, PyObject *mapping,
! PyObject **outobj, int *outsize, Py_UNICODE **outp, PyObject **res)
{
! if (charmaptranslate_lookup(c, mapping, res))
return -1;
if (*res==NULL) {
/* not found => default to 1:1 mapping */
! *(*outp)++ = (Py_UNICODE)c;
}
else if (*res==Py_None)
--- 3246,3258 ----
Return 0 on success, -1 on error. */
static
! int charmaptranslate_output(const Py_UNICODE *startinp, const Py_UNICODE *curinp,
! int insize, PyObject *mapping, PyObject **outobj, Py_UNICODE **outp,
! PyObject **res)
{
! if (charmaptranslate_lookup(*curinp, mapping, res))
return -1;
if (*res==NULL) {
/* not found => default to 1:1 mapping */
! *(*outp)++ = *curinp;
}
else if (*res==Py_None)
***************
*** 3269,3274 ****
else if (repsize!=0) {
/* more than one character */
! int requiredsize = *outsize + repsize - 1;
! if (charmaptranslate_makespace(outobj, outp, outsize, requiredsize))
return -1;
memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize);
--- 3270,3277 ----
else if (repsize!=0) {
/* more than one character */
! int requiredsize = (*outp-PyUnicode_AS_UNICODE(*outobj)) +
! (insize - (*curinp-*startinp)) +
! repsize - 1;
! if (charmaptranslate_makespace(outobj, outp, requiredsize))
return -1;
memcpy(*outp, PyUnicode_AS_UNICODE(*res), sizeof(Py_UNICODE)*repsize);
***************
*** 3295,3299 ****
/* current output position */
int respos = 0;
- int ressize;
char *reason = "character maps to <undefined>";
PyObject *errorHandler = NULL;
--- 3298,3301 ----
***************
*** 3313,3326 ****
res = PyUnicode_FromUnicode(NULL, size);
if (res == NULL)
! goto onError;
if (size == 0)
return res;
str = PyUnicode_AS_UNICODE(res);
- ressize = size;
while (p<endp) {
/* try to encode it */
PyObject *x = NULL;
! if (charmaptranslate_output(*p, mapping, &res, &ressize, &str, &x)) {
Py_XDECREF(x);
goto onError;
--- 3315,3327 ----
res = PyUnicode_FromUnicode(NULL, size);
if (res == NULL)
! goto onError;
if (size == 0)
return res;
str = PyUnicode_AS_UNICODE(res);
while (p<endp) {
/* try to encode it */
PyObject *x = NULL;
! if (charmaptranslate_output(startp, p, size, mapping, &res, &str, &x)) {
Py_XDECREF(x);
goto onError;
***************
*** 3341,3345 ****
/* find all untranslatable characters */
while (collend < endp) {
! if (charmaptranslate_lookup(*collend, mapping, &x))
goto onError;
Py_XDECREF(x);
--- 3342,3346 ----
/* find all untranslatable characters */
while (collend < endp) {
! if (charmaptranslate_lookup(*collend, mapping, &x))
goto onError;
Py_XDECREF(x);
***************
*** 3380,3384 ****
char *cp;
sprintf(buffer, "&#%d;", (int)*p);
! if (charmaptranslate_makespace(&res, &str, &ressize,
(str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend)))
goto onError;
--- 3381,3385 ----
char *cp;
sprintf(buffer, "&#%d;", (int)*p);
! if (charmaptranslate_makespace(&res, &str,
(str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend)))
goto onError;
***************
*** 3396,3400 ****
/* generate replacement */
repsize = PyUnicode_GET_SIZE(repunicode);
! if (charmaptranslate_makespace(&res, &str, &ressize,
(str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) {
Py_DECREF(repunicode);
--- 3397,3401 ----
/* generate replacement */
repsize = PyUnicode_GET_SIZE(repunicode);
! if (charmaptranslate_makespace(&res, &str,
(str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) {
Py_DECREF(repunicode);
***************
*** 3410,3414 ****
/* Resize if we allocated to much */
respos = str-PyUnicode_AS_UNICODE(res);
! if (respos<ressize) {
if (_PyUnicode_Resize(&res, respos) < 0)
goto onError;
--- 3411,3415 ----
/* Resize if we allocated to much */
respos = str-PyUnicode_AS_UNICODE(res);
! if (respos<PyUnicode_GET_SIZE(res)) {
if (_PyUnicode_Resize(&res, respos) < 0)
goto onError;
More information about the Python-checkins
mailing list