diff options
author | Walter Dörwald <walter@livinglogic.de> | 2003-01-31 17:19:08 (GMT) |
---|---|---|
committer | Walter Dörwald <walter@livinglogic.de> | 2003-01-31 17:19:08 (GMT) |
commit | 2e0b18af3092a24c9689f72af898083ebfd9aec7 (patch) | |
tree | 5dd08384e3560210deb41201ae3d7835899b44cd /Objects/unicodeobject.c | |
parent | f7f4517faeec7a37f5ad545092252a55e0450e10 (diff) | |
download | cpython-2e0b18af3092a24c9689f72af898083ebfd9aec7.zip cpython-2e0b18af3092a24c9689f72af898083ebfd9aec7.tar.gz cpython-2e0b18af3092a24c9689f72af898083ebfd9aec7.tar.bz2 |
Change the treatment of positions returned by PEP293
error handers in the Unicode codecs: Negative
positions are treated as being relative to the end of
the input and out of bounds positions result in an
IndexError.
Also update the PEP and include an explanation of
this in the documentation for codecs.register_error.
Fixes a small bug in iconv_codecs: if the position
from the callback is negative *add* it to the size
instead of substracting it.
From SF patch #677429.
Diffstat (limited to 'Objects/unicodeobject.c')
-rw-r--r-- | Objects/unicodeobject.c | 26 |
1 files changed, 17 insertions, 9 deletions
diff --git a/Objects/unicodeobject.c b/Objects/unicodeobject.c index 1abef89..dfeabf5 100644 --- a/Objects/unicodeobject.c +++ b/Objects/unicodeobject.c @@ -728,9 +728,11 @@ int unicode_decode_call_errorhandler(const char *errors, PyObject **errorHandler if (!PyArg_ParseTuple(restuple, argparse, &PyUnicode_Type, &repunicode, &newpos)) goto onError; if (newpos<0) - newpos = 0; - else if (newpos>insize) - newpos = insize; + newpos = insize+newpos; + if (newpos<0 || newpos>insize) { + PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", newpos); + goto onError; + } /* need more space? (at least enough for what we have+the replacement+the rest of the string (starting @@ -2246,9 +2248,12 @@ static PyObject *unicode_encode_call_errorhandler(const char *errors, return NULL; } if (*newpos<0) - *newpos = 0; - else if (*newpos>size) - *newpos = size; + *newpos = size+*newpos; + if (*newpos<0 || *newpos>size) { + PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", *newpos); + Py_DECREF(restuple); + return NULL; + } Py_INCREF(resunicode); Py_DECREF(restuple); return resunicode; @@ -3084,9 +3089,12 @@ static PyObject *unicode_translate_call_errorhandler(const char *errors, return NULL; } if (*newpos<0) - *newpos = 0; - else if (*newpos>size) - *newpos = size; + *newpos = size+*newpos; + if (*newpos<0 || *newpos>size) { + PyErr_Format(PyExc_IndexError, "position %d from error handler out of bounds", *newpos); + Py_DECREF(restuple); + return NULL; + } Py_INCREF(resunicode); Py_DECREF(restuple); return resunicode; |