Kaydet (Commit) 2d9856d6 authored tarafından Eric Smith's avatar Eric Smith

Issue #7309: Unchecked pointer access when converting UnicodeEncodeError,…

Issue #7309: Unchecked pointer access when converting UnicodeEncodeError, UnicodeDecodeError, and UnicodeTranslateError to strings.
üst ebc73dce
...@@ -398,6 +398,43 @@ class ExceptionTests(unittest.TestCase): ...@@ -398,6 +398,43 @@ class ExceptionTests(unittest.TestCase):
self.assertTrue(unicode(Exception(u'a'))) self.assertTrue(unicode(Exception(u'a')))
self.assertTrue(unicode(Exception(u'\xe1'))) self.assertTrue(unicode(Exception(u'\xe1')))
def testUnicodeChangeAttributes(self):
# See issue 7309. This was a crasher.
u = UnicodeEncodeError('baz', u'xxxxx', 1, 5, 'foo')
self.assertEqual(str(u), "'baz' codec can't encode characters in position 1-4: foo")
u.end = 2
self.assertEqual(str(u), "'baz' codec can't encode character u'\\x78' in position 1: foo")
u.end = 5
u.reason = 0x345345345345345345
self.assertEqual(str(u), "'baz' codec can't encode characters in position 1-4: 965230951443685724997")
u.encoding = 4000
self.assertEqual(str(u), "'4000' codec can't encode characters in position 1-4: 965230951443685724997")
u.start = 1000
self.assertEqual(str(u), "'4000' codec can't encode characters in position 1000-4: 965230951443685724997")
u = UnicodeDecodeError('baz', 'xxxxx', 1, 5, 'foo')
self.assertEqual(str(u), "'baz' codec can't decode bytes in position 1-4: foo")
u.end = 2
self.assertEqual(str(u), "'baz' codec can't decode byte 0x78 in position 1: foo")
u.end = 5
u.reason = 0x345345345345345345
self.assertEqual(str(u), "'baz' codec can't decode bytes in position 1-4: 965230951443685724997")
u.encoding = 4000
self.assertEqual(str(u), "'4000' codec can't decode bytes in position 1-4: 965230951443685724997")
u.start = 1000
self.assertEqual(str(u), "'4000' codec can't decode bytes in position 1000-4: 965230951443685724997")
u = UnicodeTranslateError(u'xxxx', 1, 5, 'foo')
self.assertEqual(str(u), "can't translate characters in position 1-4: foo")
u.end = 2
self.assertEqual(str(u), "can't translate character u'\\x78' in position 1: foo")
u.end = 5
u.reason = 0x345345345345345345
self.assertEqual(str(u), "can't translate characters in position 1-4: 965230951443685724997")
u.start = 1000
self.assertEqual(str(u), "can't translate characters in position 1000-4: 965230951443685724997")
def test_badisinstance(self): def test_badisinstance(self):
# Bug #2542: if issubclass(e, MyException) raises an exception, # Bug #2542: if issubclass(e, MyException) raises an exception,
# it should be ignored # it should be ignored
......
...@@ -12,6 +12,10 @@ What's New in Python 2.7 alpha 4? ...@@ -12,6 +12,10 @@ What's New in Python 2.7 alpha 4?
Core and Builtins Core and Builtins
----------------- -----------------
- Issue #7309: Fix unchecked attribute access when converting
UnicodeEncodeError, UnicodeDecodeError, and UnicodeTranslateError to
strings.
- Issue #7649: Fix u'%c' % char for character in range 0x80..0xFF, raise an - Issue #7649: Fix u'%c' % char for character in range 0x80..0xFF, raise an
UnicodeDecodeError UnicodeDecodeError
......
...@@ -1640,8 +1640,20 @@ static PyObject * ...@@ -1640,8 +1640,20 @@ static PyObject *
UnicodeEncodeError_str(PyObject *self) UnicodeEncodeError_str(PyObject *self)
{ {
PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
PyObject *result = NULL;
if (uself->end==uself->start+1) { PyObject *reason_str = NULL;
PyObject *encoding_str = NULL;
/* Get reason and encoding as strings, which they might not be if
they've been modified after we were contructed. */
reason_str = PyObject_Str(uself->reason);
if (reason_str == NULL)
goto done;
encoding_str = PyObject_Str(uself->encoding);
if (encoding_str == NULL)
goto done;
if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start]; int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
char badchar_str[20]; char badchar_str[20];
if (badchar <= 0xff) if (badchar <= 0xff)
...@@ -1650,21 +1662,25 @@ UnicodeEncodeError_str(PyObject *self) ...@@ -1650,21 +1662,25 @@ UnicodeEncodeError_str(PyObject *self)
PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar); PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
else else
PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar); PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
return PyString_FromFormat( result = PyString_FromFormat(
"'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s", "'%.400s' codec can't encode character u'\\%s' in position %zd: %.400s",
PyString_AS_STRING(uself->encoding), PyString_AS_STRING(encoding_str),
badchar_str, badchar_str,
uself->start, uself->start,
PyString_AS_STRING(uself->reason) PyString_AS_STRING(reason_str));
); }
else {
result = PyString_FromFormat(
"'%.400s' codec can't encode characters in position %zd-%zd: %.400s",
PyString_AS_STRING(encoding_str),
uself->start,
uself->end-1,
PyString_AS_STRING(reason_str));
} }
return PyString_FromFormat( done:
"'%.400s' codec can't encode characters in position %zd-%zd: %.400s", Py_XDECREF(reason_str);
PyString_AS_STRING(uself->encoding), Py_XDECREF(encoding_str);
uself->start, return result;
uself->end-1,
PyString_AS_STRING(uself->reason)
);
} }
static PyTypeObject _PyExc_UnicodeEncodeError = { static PyTypeObject _PyExc_UnicodeEncodeError = {
...@@ -1709,27 +1725,43 @@ static PyObject * ...@@ -1709,27 +1725,43 @@ static PyObject *
UnicodeDecodeError_str(PyObject *self) UnicodeDecodeError_str(PyObject *self)
{ {
PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
PyObject *result = NULL;
if (uself->end==uself->start+1) { PyObject *reason_str = NULL;
PyObject *encoding_str = NULL;
/* Get reason and encoding as strings, which they might not be if
they've been modified after we were contructed. */
reason_str = PyObject_Str(uself->reason);
if (reason_str == NULL)
goto done;
encoding_str = PyObject_Str(uself->encoding);
if (encoding_str == NULL)
goto done;
if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
/* FromFormat does not support %02x, so format that separately */ /* FromFormat does not support %02x, so format that separately */
char byte[4]; char byte[4];
PyOS_snprintf(byte, sizeof(byte), "%02x", PyOS_snprintf(byte, sizeof(byte), "%02x",
((int)PyString_AS_STRING(uself->object)[uself->start])&0xff); ((int)PyString_AS_STRING(uself->object)[uself->start])&0xff);
return PyString_FromFormat( result = PyString_FromFormat(
"'%.400s' codec can't decode byte 0x%s in position %zd: %.400s", "'%.400s' codec can't decode byte 0x%s in position %zd: %.400s",
PyString_AS_STRING(uself->encoding), PyString_AS_STRING(encoding_str),
byte, byte,
uself->start, uself->start,
PyString_AS_STRING(uself->reason) PyString_AS_STRING(reason_str));
);
} }
return PyString_FromFormat( else {
"'%.400s' codec can't decode bytes in position %zd-%zd: %.400s", result = PyString_FromFormat(
PyString_AS_STRING(uself->encoding), "'%.400s' codec can't decode bytes in position %zd-%zd: %.400s",
uself->start, PyString_AS_STRING(encoding_str),
uself->end-1, uself->start,
PyString_AS_STRING(uself->reason) uself->end-1,
); PyString_AS_STRING(reason_str));
}
done:
Py_XDECREF(reason_str);
Py_XDECREF(encoding_str);
return result;
} }
static PyTypeObject _PyExc_UnicodeDecodeError = { static PyTypeObject _PyExc_UnicodeDecodeError = {
...@@ -1794,8 +1826,16 @@ static PyObject * ...@@ -1794,8 +1826,16 @@ static PyObject *
UnicodeTranslateError_str(PyObject *self) UnicodeTranslateError_str(PyObject *self)
{ {
PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self; PyUnicodeErrorObject *uself = (PyUnicodeErrorObject *)self;
PyObject *result = NULL;
PyObject *reason_str = NULL;
if (uself->end==uself->start+1) { /* Get reason as a string, which it might not be if it's been
modified after we were contructed. */
reason_str = PyObject_Str(uself->reason);
if (reason_str == NULL)
goto done;
if (uself->start < PyUnicode_GET_SIZE(uself->object) && uself->end == uself->start+1) {
int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start]; int badchar = (int)PyUnicode_AS_UNICODE(uself->object)[uself->start];
char badchar_str[20]; char badchar_str[20];
if (badchar <= 0xff) if (badchar <= 0xff)
...@@ -1804,19 +1844,21 @@ UnicodeTranslateError_str(PyObject *self) ...@@ -1804,19 +1844,21 @@ UnicodeTranslateError_str(PyObject *self)
PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar); PyOS_snprintf(badchar_str, sizeof(badchar_str), "u%04x", badchar);
else else
PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar); PyOS_snprintf(badchar_str, sizeof(badchar_str), "U%08x", badchar);
return PyString_FromFormat( result = PyString_FromFormat(
"can't translate character u'\\%s' in position %zd: %.400s", "can't translate character u'\\%s' in position %zd: %.400s",
badchar_str, badchar_str,
uself->start, uself->start,
PyString_AS_STRING(uself->reason) PyString_AS_STRING(reason_str));
); } else {
result = PyString_FromFormat(
"can't translate characters in position %zd-%zd: %.400s",
uself->start,
uself->end-1,
PyString_AS_STRING(reason_str));
} }
return PyString_FromFormat( done:
"can't translate characters in position %zd-%zd: %.400s", Py_XDECREF(reason_str);
uself->start, return result;
uself->end-1,
PyString_AS_STRING(uself->reason)
);
} }
static PyTypeObject _PyExc_UnicodeTranslateError = { static PyTypeObject _PyExc_UnicodeTranslateError = {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment