File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 11from collections import OrderedDict
22from test .test_json import PyTest , CTest
3+ from test .support import bigaddrspacetest
34
45
56CASES = [
@@ -38,4 +39,10 @@ def test_sorted_dict(self):
3839
3940
4041class TestPyEncodeBasestringAscii (TestEncodeBasestringAscii , PyTest ): pass
41- class TestCEncodeBasestringAscii (TestEncodeBasestringAscii , CTest ): pass
42+ class TestCEncodeBasestringAscii (TestEncodeBasestringAscii , CTest ):
43+ @bigaddrspacetest
44+ def test_overflow (self ):
45+ s = "\uffff " * ((2 ** 32 )// 6 + 1 )
46+ with self .assertRaises (OverflowError ):
47+ self .json .encoder .encode_basestring_ascii (s )
48+
Original file line number Diff line number Diff line change @@ -229,6 +229,9 @@ Library
229229- Issue #23326: Removed __ne__ implementations. Since fixing default __ne__
230230 implementation in issue #21408 they are redundant.
231231
232+ - Issue #23369: Fixed possible integer overflow in
233+ _json.encode_basestring_ascii.
234+
232235- Issue #23353: Fix the exception handling of generators in
233236 PyEval_EvalFrameEx(). At entry, save or swap the exception state even if
234237 PyEval_EvalFrameEx() is called with throwflag=0. At exit, the exception state
Original file line number Diff line number Diff line change @@ -182,17 +182,24 @@ ascii_escape_unicode(PyObject *pystr)
182182 /* Compute the output size */
183183 for (i = 0 , output_size = 2 ; i < input_chars ; i ++ ) {
184184 Py_UCS4 c = PyUnicode_READ (kind , input , i );
185- if (S_CHAR (c ))
186- output_size ++ ;
185+ Py_ssize_t d ;
186+ if (S_CHAR (c )) {
187+ d = 1 ;
188+ }
187189 else {
188190 switch (c ) {
189191 case '\\' : case '"' : case '\b' : case '\f' :
190192 case '\n' : case '\r' : case '\t' :
191- output_size + = 2 ; break ;
193+ d = 2 ; break ;
192194 default :
193- output_size + = c >= 0x10000 ? 12 : 6 ;
195+ d = c >= 0x10000 ? 12 : 6 ;
194196 }
195197 }
198+ if (output_size > PY_SSIZE_T_MAX - d ) {
199+ PyErr_SetString (PyExc_OverflowError , "string is too long to escape" );
200+ return NULL ;
201+ }
202+ output_size += d ;
196203 }
197204
198205 rval = PyUnicode_New (output_size , 127 );
You can’t perform that action at this time.
0 commit comments