Skip to content

Commit

Permalink
[3.8] gh-101180: Fix a bug where iso2022_jp_3 and iso2022_jp_2004 cod…
Browse files Browse the repository at this point in the history
…ecs read out of bounds (gh-111695)

(cherry picked from commit c8faa35)

Co-authored-by: Masayuki Moriyama <[email protected]>
  • Loading branch information
moriyama authored and ambv committed Nov 6, 2023
1 parent edf701c commit 4b3b99b
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 3 deletions.
46 changes: 46 additions & 0 deletions Lib/test/test_codecencodings_iso2022.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,52 @@ class Test_ISO2022_JP2(multibytecodec_support.TestBase, unittest.TestCase):
(b'ab\x1BNdef', 'replace', 'abdef'),
)

class Test_ISO2022_JP3(multibytecodec_support.TestBase, unittest.TestCase):
encoding = 'iso2022_jp_3'
tstring = multibytecodec_support.load_teststring('iso2022_jp')
codectests = COMMON_CODEC_TESTS + (
(b'ab\x1BNdef', 'replace', 'ab\x1BNdef'),
(b'\x1B$(O\x2E\x23\x1B(B', 'strict', '\u3402' ),
(b'\x1B$(O\x2E\x22\x1B(B', 'strict', '\U0002000B' ),
(b'\x1B$(O\x24\x77\x1B(B', 'strict', '\u304B\u309A'),
(b'\x1B$(P\x21\x22\x1B(B', 'strict', '\u4E02' ),
(b'\x1B$(P\x7E\x76\x1B(B', 'strict', '\U0002A6B2' ),
('\u3402', 'strict', b'\x1B$(O\x2E\x23\x1B(B'),
('\U0002000B', 'strict', b'\x1B$(O\x2E\x22\x1B(B'),
('\u304B\u309A', 'strict', b'\x1B$(O\x24\x77\x1B(B'),
('\u4E02', 'strict', b'\x1B$(P\x21\x22\x1B(B'),
('\U0002A6B2', 'strict', b'\x1B$(P\x7E\x76\x1B(B'),
(b'ab\x1B$(O\x2E\x21\x1B(Bdef', 'replace', 'ab\uFFFDdef'),
('ab\u4FF1def', 'replace', b'ab?def'),
)
xmlcharnametest = (
'\xAB\u211C\xBB = \u2329\u1234\u232A',
b'\x1B$(O\x29\x28\x1B(B&real;\x1B$(O\x29\x32\x1B(B = &lang;&#4660;&rang;'
)

class Test_ISO2022_JP2004(multibytecodec_support.TestBase, unittest.TestCase):
encoding = 'iso2022_jp_2004'
tstring = multibytecodec_support.load_teststring('iso2022_jp')
codectests = COMMON_CODEC_TESTS + (
(b'ab\x1BNdef', 'replace', 'ab\x1BNdef'),
(b'\x1B$(Q\x2E\x23\x1B(B', 'strict', '\u3402' ),
(b'\x1B$(Q\x2E\x22\x1B(B', 'strict', '\U0002000B' ),
(b'\x1B$(Q\x24\x77\x1B(B', 'strict', '\u304B\u309A'),
(b'\x1B$(P\x21\x22\x1B(B', 'strict', '\u4E02' ),
(b'\x1B$(P\x7E\x76\x1B(B', 'strict', '\U0002A6B2' ),
('\u3402', 'strict', b'\x1B$(Q\x2E\x23\x1B(B'),
('\U0002000B', 'strict', b'\x1B$(Q\x2E\x22\x1B(B'),
('\u304B\u309A', 'strict', b'\x1B$(Q\x24\x77\x1B(B'),
('\u4E02', 'strict', b'\x1B$(P\x21\x22\x1B(B'),
('\U0002A6B2', 'strict', b'\x1B$(P\x7E\x76\x1B(B'),
(b'ab\x1B$(Q\x2E\x21\x1B(Bdef', 'replace', 'ab\u4FF1def'),
('ab\u4FF1def', 'replace', b'ab\x1B$(Q\x2E\x21\x1B(Bdef'),
)
xmlcharnametest = (
'\xAB\u211C\xBB = \u2329\u1234\u232A',
b'\x1B$(Q\x29\x28\x1B(B&real;\x1B$(Q\x29\x32\x1B(B = &lang;&#4660;&rang;'
)

class Test_ISO2022_KR(multibytecodec_support.TestBase, unittest.TestCase):
encoding = 'iso2022_kr'
tstring = multibytecodec_support.load_teststring('iso2022_kr')
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix a bug where ``iso2022_jp_3`` and ``iso2022_jp_2004`` codecs read out of bounds
9 changes: 6 additions & 3 deletions Modules/cjkcodecs/_codecs_iso2022.c
Original file line number Diff line number Diff line change
Expand Up @@ -181,8 +181,9 @@ ENCODER(iso2022)

encoded = MAP_UNMAPPABLE;
for (dsg = CONFIG_DESIGNATIONS; dsg->mark; dsg++) {
Py_UCS4 buf[2] = {c, 0};
Py_ssize_t length = 1;
encoded = dsg->encoder(&c, &length);
encoded = dsg->encoder(buf, &length);
if (encoded == MAP_MULTIPLE_AVAIL) {
/* this implementation won't work for pair
* of non-bmp characters. */
Expand All @@ -191,9 +192,11 @@ ENCODER(iso2022)
return MBERR_TOOFEW;
length = -1;
}
else
else {
buf[1] = INCHAR2;
length = 2;
encoded = dsg->encoder(&c, &length);
}
encoded = dsg->encoder(buf, &length);
if (encoded != MAP_UNMAPPABLE) {
insize = length;
break;
Expand Down

0 comments on commit 4b3b99b

Please sign in to comment.