Skip to content

Commit 1b8abbd

Browse files
committed
merge 3.4 (#22519)
2 parents 42db725 + fef2857 commit 1b8abbd

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

Misc/NEWS

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ Release date: TBA
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #22519: Fix overflow checking in PyBytes_Repr.
14+
1315
- Issue #22518: Fix integer overflow issues in latin-1 encoding.
1416

1517
- Issue #16324: _charset parameter of MIMEText now also accepts

Objects/bytesobject.c

Lines changed: 16 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -629,28 +629,27 @@ PyBytes_Repr(PyObject *obj, int smartquotes)
629629
newsize = 3; /* b'' */
630630
s = (unsigned char*)op->ob_sval;
631631
for (i = 0; i < length; i++) {
632+
Py_ssize_t incr = 1;
632633
switch(s[i]) {
633-
case '\'': squotes++; newsize++; break;
634-
case '"': dquotes++; newsize++; break;
634+
case '\'': squotes++; break;
635+
case '"': dquotes++; break;
635636
case '\\': case '\t': case '\n': case '\r':
636-
newsize += 2; break; /* \C */
637+
incr = 2; break; /* \C */
637638
default:
638639
if (s[i] < ' ' || s[i] >= 0x7f)
639-
newsize += 4; /* \xHH */
640-
else
641-
newsize++;
640+
incr = 4; /* \xHH */
642641
}
642+
if (newsize > PY_SSIZE_T_MAX - incr)
643+
goto overflow;
644+
newsize += incr;
643645
}
644646
quote = '\'';
645647
if (smartquotes && squotes && !dquotes)
646648
quote = '"';
647-
if (squotes && quote == '\'')
649+
if (squotes && quote == '\'') {
650+
if (newsize > PY_SSIZE_T_MAX - squotes)
651+
goto overflow;
648652
newsize += squotes;
649-
650-
if (newsize > (PY_SSIZE_T_MAX - sizeof(PyUnicodeObject) - 1)) {
651-
PyErr_SetString(PyExc_OverflowError,
652-
"bytes object is too large to make repr");
653-
return NULL;
654653
}
655654

656655
v = PyUnicode_New(newsize, 127);
@@ -682,6 +681,11 @@ PyBytes_Repr(PyObject *obj, int smartquotes)
682681
*p++ = quote;
683682
assert(_PyUnicode_CheckConsistency(v, 1));
684683
return v;
684+
685+
overflow:
686+
PyErr_SetString(PyExc_OverflowError,
687+
"bytes object is too large to make repr");
688+
return NULL;
685689
}
686690

687691
static PyObject *

0 commit comments

Comments
 (0)