Skip to content

Commit 0f9b668

Browse files
bpo-35372: Fix the code page decoder for input > 2 GiB. (GH-10848)
(cherry picked from commit 4013c17) Co-authored-by: Serhiy Storchaka <storchaka@gmail.com>
1 parent f65ede3 commit 0f9b668

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
@@ -3191,6 +3191,24 @@ def _get_fake_codepage(*a):
31913191
finally:
31923192
_bootlocale.getpreferredencoding = old_getpreferredencoding
31933193

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

31953213
class ASCIITest(unittest.TestCase):
31963214
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
@@ -7285,7 +7285,7 @@ decode_code_page_errors(UINT code_page,
72857285
"in the target code page.";
72867286
/* each step cannot decode more than 1 character, but a character can be
72877287
represented as a surrogate pair */
7288-
wchar_t buffer[2], *startout, *out;
7288+
wchar_t buffer[2], *out;
72897289
int insize;
72907290
Py_ssize_t outsize;
72917291
PyObject *errorHandler = NULL;
@@ -7322,7 +7322,7 @@ decode_code_page_errors(UINT code_page,
73227322
*v = (PyObject*)_PyUnicode_New(size * Py_ARRAY_LENGTH(buffer));
73237323
if (*v == NULL)
73247324
goto error;
7325-
startout = PyUnicode_AS_UNICODE(*v);
7325+
out = PyUnicode_AS_UNICODE(*v);
73267326
}
73277327
else {
73287328
/* Extend unicode object */
@@ -7333,11 +7333,10 @@ decode_code_page_errors(UINT code_page,
73337333
}
73347334
if (unicode_resize(v, n + size * Py_ARRAY_LENGTH(buffer)) < 0)
73357335
goto error;
7336-
startout = PyUnicode_AS_UNICODE(*v) + n;
7336+
out = PyUnicode_AS_UNICODE(*v) + n;
73377337
}
73387338

73397339
/* Decode the byte string character per character */
7340-
out = startout;
73417340
while (in < endin)
73427341
{
73437342
/* Decode a character */
@@ -7392,7 +7391,7 @@ decode_code_page_errors(UINT code_page,
73927391
*out = 0;
73937392

73947393
/* Extend unicode object */
7395-
outsize = out - startout;
7394+
outsize = out - PyUnicode_AS_UNICODE(*v);
73967395
assert(outsize <= PyUnicode_WSTR_LENGTH(*v));
73977396
if (unicode_resize(v, outsize) < 0)
73987397
goto error;

0 commit comments

Comments
 (0)