Skip to content

Commit a164e34

Browse files
committed
merge 3.3 (#22520)
2 parents af3f494 + 14658f6 commit a164e34

2 files changed

Lines changed: 20 additions & 11 deletions

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,9 @@ What's New in Python 3.4.3?
99
Core and Builtins
1010
-----------------
1111

12+
- Issue #22520: Fix overflow checking when generating the repr of a unicode
13+
object.
14+
1215
- Issue #22519: Fix overflow checking in PyBytes_Repr.
1316

1417
- Issue #22518: Fix integer overflow issues in latin-1 encoding.

Objects/unicodeobject.c

Lines changed: 17 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12341,28 +12341,34 @@ unicode_repr(PyObject *unicode)
1234112341
ikind = PyUnicode_KIND(unicode);
1234212342
for (i = 0; i < isize; i++) {
1234312343
Py_UCS4 ch = PyUnicode_READ(ikind, idata, i);
12344+
Py_ssize_t incr = 1;
1234412345
switch (ch) {
12345-
case '\'': squote++; osize++; break;
12346-
case '"': dquote++; osize++; break;
12346+
case '\'': squote++; break;
12347+
case '"': dquote++; break;
1234712348
case '\\': case '\t': case '\r': case '\n':
12348-
osize += 2; break;
12349+
incr = 2;
12350+
break;
1234912351
default:
1235012352
/* Fast-path ASCII */
1235112353
if (ch < ' ' || ch == 0x7f)
12352-
osize += 4; /* \xHH */
12354+
incr = 4; /* \xHH */
1235312355
else if (ch < 0x7f)
12354-
osize++;
12355-
else if (Py_UNICODE_ISPRINTABLE(ch)) {
12356-
osize++;
12356+
;
12357+
else if (Py_UNICODE_ISPRINTABLE(ch))
1235712358
max = ch > max ? ch : max;
12358-
}
1235912359
else if (ch < 0x100)
12360-
osize += 4; /* \xHH */
12360+
incr = 4; /* \xHH */
1236112361
else if (ch < 0x10000)
12362-
osize += 6; /* \uHHHH */
12362+
incr = 6; /* \uHHHH */
1236312363
else
12364-
osize += 10; /* \uHHHHHHHH */
12364+
incr = 10; /* \uHHHHHHHH */
12365+
}
12366+
if (osize > PY_SSIZE_T_MAX - incr) {
12367+
PyErr_SetString(PyExc_OverflowError,
12368+
"string is too long to generate repr");
12369+
return NULL;
1236512370
}
12371+
osize += incr;
1236612372
}
1236712373

1236812374
quote = '\'';

0 commit comments

Comments
 (0)