[Python-checkins]
python/dist/src/Objects unicodeobject.c, 2.190.6.8, 2.190.6.9
doerwalter at users.sourceforge.net
doerwalter at users.sourceforge.net
Fri Oct 24 11:05:32 EDT 2003
Update of /cvsroot/python/python/dist/src/Objects
In directory sc8-pr-cvs1:/tmp/cvs-serv28564/Objects
Modified Files:
Tag: release23-maint
unicodeobject.c
Log Message:
Backport checkin:
Fix a bug in the memory reallocation code of PyUnicode_TranslateCharmap().
charmaptranslate_makespace() allocates 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.190.6.8
retrieving revision 2.190.6.9
diff -C2 -d -r2.190.6.8 -r2.190.6.9
*** unicodeobject.c 18 Oct 2003 09:54:38 -0000 2.190.6.8
--- unicodeobject.c 24 Oct 2003 15:05:29 -0000 2.190.6.9
***************
*** 3224,3240 ****
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))
return -1;
*outp = PyUnicode_AS_UNICODE(*outobj) + outpos;
- *outsize = requiredsize;
}
return 0;
--- 3224,3240 ----
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))
return -1;
*outp = PyUnicode_AS_UNICODE(*outobj) + outpos;
}
return 0;
***************
*** 3247,3258 ****
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)
--- 3247,3259 ----
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)
***************
*** 3270,3275 ****
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);
--- 3271,3278 ----
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);
***************
*** 3296,3300 ****
/* current output position */
int respos = 0;
- int ressize;
char *reason = "character maps to <undefined>";
PyObject *errorHandler = NULL;
--- 3299,3302 ----
***************
*** 3314,3327 ****
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;
--- 3316,3328 ----
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;
***************
*** 3342,3346 ****
/* find all untranslatable characters */
while (collend < endp) {
! if (charmaptranslate_lookup(*collend, mapping, &x))
goto onError;
Py_XDECREF(x);
--- 3343,3347 ----
/* find all untranslatable characters */
while (collend < endp) {
! if (charmaptranslate_lookup(*collend, mapping, &x))
goto onError;
Py_XDECREF(x);
***************
*** 3381,3385 ****
char *cp;
sprintf(buffer, "&#%d;", (int)*p);
! if (charmaptranslate_makespace(&res, &str, &ressize,
(str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend)))
goto onError;
--- 3382,3386 ----
char *cp;
sprintf(buffer, "&#%d;", (int)*p);
! if (charmaptranslate_makespace(&res, &str,
(str-PyUnicode_AS_UNICODE(res))+strlen(buffer)+(endp-collend)))
goto onError;
***************
*** 3397,3401 ****
/* generate replacement */
repsize = PyUnicode_GET_SIZE(repunicode);
! if (charmaptranslate_makespace(&res, &str, &ressize,
(str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) {
Py_DECREF(repunicode);
--- 3398,3402 ----
/* generate replacement */
repsize = PyUnicode_GET_SIZE(repunicode);
! if (charmaptranslate_makespace(&res, &str,
(str-PyUnicode_AS_UNICODE(res))+repsize+(endp-collend))) {
Py_DECREF(repunicode);
***************
*** 3411,3415 ****
/* Resize if we allocated to much */
respos = str-PyUnicode_AS_UNICODE(res);
! if (respos<ressize) {
if (_PyUnicode_Resize(&res, respos))
goto onError;
--- 3412,3416 ----
/* Resize if we allocated to much */
respos = str-PyUnicode_AS_UNICODE(res);
! if (respos<PyUnicode_GET_SIZE(res)) {
if (_PyUnicode_Resize(&res, respos))
goto onError;
More information about the Python-checkins
mailing list