Skip to content

Commit 73dadc4

Browse files
committed
prevent integer overflow in escape_unicode (closes #24522)
1 parent c422308 commit 73dadc4

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

Misc/NEWS

+2
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Core and Builtins
2424
Library
2525
-------
2626

27+
- Issue #24522: Fix possible integer overflow in json accelerator module.
28+
2729
- Issue #24489: ensure a previously set C errno doesn't disturb cmath.polar().
2830

2931
- Issue #24408: Fixed AttributeError in measure() and metrics() methods of

Modules/_json.c

+9-3
Original file line numberDiff line numberDiff line change
@@ -249,17 +249,23 @@ escape_unicode(PyObject *pystr)
249249
/* Compute the output size */
250250
for (i = 0, output_size = 2; i < input_chars; i++) {
251251
Py_UCS4 c = PyUnicode_READ(kind, input, i);
252+
Py_ssize_t d;
252253
switch (c) {
253254
case '\\': case '"': case '\b': case '\f':
254255
case '\n': case '\r': case '\t':
255-
output_size += 2;
256+
d = 2;
256257
break;
257258
default:
258259
if (c <= 0x1f)
259-
output_size += 6;
260+
d = 6;
260261
else
261-
output_size++;
262+
d = 1;
263+
}
264+
if (output_size > PY_SSIZE_T_MAX - d) {
265+
PyErr_SetString(PyExc_OverflowError, "string is too long to escape");
266+
return NULL;
262267
}
268+
output_size += d;
263269
}
264270

265271
rval = PyUnicode_New(output_size, maxchar);

0 commit comments

Comments
 (0)