Skip to content

Commit d524922

Browse files
Issue python#22518: Fixed integer overflow issues in "backslashreplace" and
"xmlcharrefreplace" error handlers.
1 parent 52313d7 commit d524922

2 files changed

Lines changed: 15 additions & 2 deletions

File tree

‎Misc/NEWS‎

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

13+
- Issue #22518: Fixed integer overflow issues in "backslashreplace" and
14+
"xmlcharrefreplace" error handlers.
15+
1316
- Issue #22526: Fix iterating through files with lines longer than 2^31 bytes.
1417

1518
- Issue #22519: Fix overflow checking in PyString_Repr.

‎Python/codecs.c‎

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -558,14 +558,22 @@ PyObject *PyCodec_XMLCharRefReplaceErrors(PyObject *exc)
558558
Py_UNICODE *startp;
559559
Py_UNICODE *e;
560560
Py_UNICODE *outp;
561-
int ressize;
561+
Py_ssize_t ressize;
562562
if (PyUnicodeEncodeError_GetStart(exc, &start))
563563
return NULL;
564564
if (PyUnicodeEncodeError_GetEnd(exc, &end))
565565
return NULL;
566566
if (!(object = PyUnicodeEncodeError_GetObject(exc)))
567567
return NULL;
568568
startp = PyUnicode_AS_UNICODE(object);
569+
if (end - start > PY_SSIZE_T_MAX / (2+7+1)) {
570+
end = start + PY_SSIZE_T_MAX / (2+7+1);
571+
#ifndef Py_UNICODE_WIDE
572+
ch = startp[end - 1];
573+
if (0xD800 <= ch && ch <= 0xDBFF)
574+
end--;
575+
#endif
576+
}
569577
e = startp + end;
570578
for (p = startp+start, ressize = 0; p < e;) {
571579
Py_UCS4 ch = *p++;
@@ -675,13 +683,15 @@ PyObject *PyCodec_BackslashReplaceErrors(PyObject *exc)
675683
Py_UNICODE *p;
676684
Py_UNICODE *startp;
677685
Py_UNICODE *outp;
678-
int ressize;
686+
Py_ssize_t ressize;
679687
if (PyUnicodeEncodeError_GetStart(exc, &start))
680688
return NULL;
681689
if (PyUnicodeEncodeError_GetEnd(exc, &end))
682690
return NULL;
683691
if (!(object = PyUnicodeEncodeError_GetObject(exc)))
684692
return NULL;
693+
if (end - start > PY_SSIZE_T_MAX / (1+1+8))
694+
end = start + PY_SSIZE_T_MAX / (1+1+8);
685695
startp = PyUnicode_AS_UNICODE(object);
686696
for (p = startp+start, ressize = 0; p < startp+end; ++p) {
687697
#ifdef Py_UNICODE_WIDE

0 commit comments

Comments
 (0)