Skip to content

Commit 4013c17

Browse files
bpo-35372: Fix the code page decoder for input > 2 GiB. (GH-10848)
1 parent 062cbb6 commit 4013c17

3 files changed

Lines changed: 24 additions & 5 deletions

File tree

Lib/test/test_codecs.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3190,6 +3190,24 @@ def test_mbcs_alias(self):
31903190
codec = codecs.lookup('cp123')
31913191
self.assertEqual(codec.name, 'mbcs')
31923192

3193+
@support.bigmemtest(size=2**31, memuse=7, dry_run=False)
3194+
def test_large_input(self):
3195+
# Test input longer than INT_MAX.
3196+
# Input should contain undecodable bytes before and after
3197+
# the INT_MAX limit.
3198+
encoded = (b'01234567' * (2**28-1) +
3199+
b'\x85\x86\xea\xeb\xec\xef\xfc\xfd\xfe\xff')
3200+
self.assertEqual(len(encoded), 2**31+2)
3201+
decoded = codecs.code_page_decode(932, encoded, 'surrogateescape', True)
3202+
self.assertEqual(decoded[1], len(encoded))
3203+
del encoded
3204+
self.assertEqual(len(decoded[0]), decoded[1])
3205+
self.assertEqual(decoded[0][:10], '0123456701')
3206+
self.assertEqual(decoded[0][-20:],
3207+
'6701234567'
3208+
'\udc85\udc86\udcea\udceb\udcec'
3209+
'\udcef\udcfc\udcfd\udcfe\udcff')
3210+
31933211

31943212
class ASCIITest(unittest.TestCase):
31953213
def test_encode(self):
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fixed the code page decoder for input longer than 2 GiB containing
2+
undecodable bytes.

Objects/unicodeobject.c

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7211,7 +7211,7 @@ decode_code_page_errors(UINT code_page,
72117211
"in the target code page.";
72127212
/* each step cannot decode more than 1 character, but a character can be
72137213
represented as a surrogate pair */
7214-
wchar_t buffer[2], *startout, *out;
7214+
wchar_t buffer[2], *out;
72157215
int insize;
72167216
Py_ssize_t outsize;
72177217
PyObject *errorHandler = NULL;
@@ -7248,7 +7248,7 @@ decode_code_page_errors(UINT code_page,
72487248
*v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer));
72497249
if (*v == NULL)
72507250
goto error;
7251-
startout = PyUnicode_AS_UNICODE(*v);
7251+
out = PyUnicode_AS_UNICODE(*v);
72527252
}
72537253
else {
72547254
/* Extend unicode object */
@@ -7259,11 +7259,10 @@ decode_code_page_errors(UINT code_page,
72597259
}
72607260
if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0)
72617261
goto error;
7262-
startout = PyUnicode_AS_UNICODE(*v) + n;
7262+
out = PyUnicode_AS_UNICODE(*v) + n;
72637263
}
72647264

72657265
/* Decode the byte string character per character */
7266-
out = startout;
72677266
while (in < endin)
72687267
{
72697268
/* Decode a character */
@@ -7318,7 +7317,7 @@ decode_code_page_errors(UINT code_page,
73187317
*out = 0;
73197318

73207319
/* Extend unicode object */
7321-
outsize = out - startout;
7320+
outsize = out - PyUnicode_AS_UNICODE(*v);
73227321
assert(outsize <= PyUnicode_WSTR_LENGTH(*v));
73237322
if (unicode_resize(v, outsize) < 0)
73247323
goto error;

0 commit comments

Comments
 (0)