Commit 3675cd9d authored by Benjamin Peterson's avatar Benjamin Peterson

merge 3.3 (#23369)

parents 3a43d063 e3bfe193
from collections import OrderedDict from collections import OrderedDict
from test.test_json import PyTest, CTest from test.test_json import PyTest, CTest
from test.support import bigaddrspacetest
CASES = [ CASES = [
...@@ -41,4 +42,10 @@ class TestEncodeBasestringAscii: ...@@ -41,4 +42,10 @@ class TestEncodeBasestringAscii:
class TestPyEncodeBasestringAscii(TestEncodeBasestringAscii, PyTest): pass class TestPyEncodeBasestringAscii(TestEncodeBasestringAscii, PyTest): pass
class TestCEncodeBasestringAscii(TestEncodeBasestringAscii, CTest): pass class TestCEncodeBasestringAscii(TestEncodeBasestringAscii, CTest):
@bigaddrspacetest
def test_overflow(self):
s = "\uffff"*((2**32)//6 + 1)
with self.assertRaises(OverflowError):
self.json.encoder.encode_basestring_ascii(s)
...@@ -50,6 +50,9 @@ Core and Builtins ...@@ -50,6 +50,9 @@ Core and Builtins
Library Library
------- -------
- Issue #23369: Fixed possible integer overflow in
_json.encode_basestring_ascii.
- Issue #23353: Fix the exception handling of generators in - Issue #23353: Fix the exception handling of generators in
PyEval_EvalFrameEx(). At entry, save or swap the exception state even if PyEval_EvalFrameEx(). At entry, save or swap the exception state even if
PyEval_EvalFrameEx() is called with throwflag=0. At exit, the exception state PyEval_EvalFrameEx() is called with throwflag=0. At exit, the exception state
......
...@@ -182,17 +182,24 @@ ascii_escape_unicode(PyObject *pystr) ...@@ -182,17 +182,24 @@ ascii_escape_unicode(PyObject *pystr)
/* Compute the output size */ /* Compute the output size */
for (i = 0, output_size = 2; i < input_chars; i++) { for (i = 0, output_size = 2; i < input_chars; i++) {
Py_UCS4 c = PyUnicode_READ(kind, input, i); Py_UCS4 c = PyUnicode_READ(kind, input, i);
if (S_CHAR(c)) Py_ssize_t d;
output_size++; if (S_CHAR(c)) {
d = 1;
}
else { else {
switch(c) { switch(c) {
case '\\': case '"': case '\b': case '\f': case '\\': case '"': case '\b': case '\f':
case '\n': case '\r': case '\t': case '\n': case '\r': case '\t':
output_size += 2; break; d = 2; break;
default: default:
output_size += c >= 0x10000 ? 12 : 6; d = c >= 0x10000 ? 12 : 6;
} }
} }
if (output_size > PY_SSIZE_T_MAX - d) {
PyErr_SetString(PyExc_OverflowError, "string is too long to escape");
return NULL;
}
output_size += d;
} }
rval = PyUnicode_New(output_size, 127); rval = PyUnicode_New(output_size, 127);
......
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