Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3fd4ab3

Browse files
Issue #17043: The unicode-internal decoder no longer read past the end of
input buffer.
1 parent df4bb46 commit 3fd4ab3

2 files changed

Lines changed: 27 additions & 27 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 3.2.4
1010
Core and Builtins
1111
-----------------
1212

13+
- Issue #17043: The unicode-internal decoder no longer read past the end of
14+
input buffer.
15+
1316
- Issue #16979: Fix error handling bugs in the unicode-escape-decode decoder.
1417

1518
- Issue #10156: In the interpreter's initialization phase, unicode globals

Objects/unicodeobject.c

Lines changed: 24 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -4392,37 +4392,34 @@ PyObject *_PyUnicode_DecodeUnicodeInternal(const char *s,
43924392
end = s + size;
43934393

43944394
while (s < end) {
4395+
if (end-s < Py_UNICODE_SIZE) {
4396+
endinpos = end-starts;
4397+
reason = "truncated input";
4398+
goto error;
4399+
}
43954400
memcpy(p, s, sizeof(Py_UNICODE));
4401+
#ifdef Py_UNICODE_WIDE
43964402
/* We have to sanity check the raw data, otherwise doom looms for
43974403
some malformed UCS-4 data. */
4398-
if (
4399-
#ifdef Py_UNICODE_WIDE
4400-
*p > unimax || *p < 0 ||
4401-
#endif
4402-
end-s < Py_UNICODE_SIZE
4403-
)
4404-
{
4405-
startinpos = s - starts;
4406-
if (end-s < Py_UNICODE_SIZE) {
4407-
endinpos = end-starts;
4408-
reason = "truncated input";
4409-
}
4410-
else {
4411-
endinpos = s - starts + Py_UNICODE_SIZE;
4412-
reason = "illegal code point (> 0x10FFFF)";
4413-
}
4414-
outpos = p - PyUnicode_AS_UNICODE(v);
4415-
if (unicode_decode_call_errorhandler(
4416-
errors, &errorHandler,
4417-
"unicode_internal", reason,
4418-
&starts, &end, &startinpos, &endinpos, &exc, &s,
4419-
&v, &outpos, &p)) {
4420-
goto onError;
4421-
}
4404+
if (*p > unimax || *p < 0) {
4405+
endinpos = s - starts + Py_UNICODE_SIZE;
4406+
reason = "illegal code point (> 0x10FFFF)";
4407+
goto error;
44224408
}
4423-
else {
4424-
p++;
4425-
s += Py_UNICODE_SIZE;
4409+
#endif
4410+
p++;
4411+
s += Py_UNICODE_SIZE;
4412+
continue;
4413+
4414+
error:
4415+
startinpos = s - starts;
4416+
outpos = p - PyUnicode_AS_UNICODE(v);
4417+
if (unicode_decode_call_errorhandler(
4418+
errors, &errorHandler,
4419+
"unicode_internal", reason,
4420+
&starts, &end, &startinpos, &endinpos, &exc, &s,
4421+
&v, &outpos, &p)) {
4422+
goto onError;
44264423
}
44274424
}
44284425

0 commit comments

Comments
 (0)