Skip to content

Commit 0c6d2f6

Browse files
authored
gh-148093: Raise binascii.Error from binascii.a2b_uu() on empty input (GH-149077)
Instead of reading past the end of the empty buffer.
1 parent 3506125 commit 0c6d2f6

3 files changed

Lines changed: 17 additions & 0 deletions

File tree

Lib/test/test_binascii.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1306,6 +1306,10 @@ def test_uu(self):
13061306
self.assertEqual(binascii.a2b_uu(b"\xff"), b"\x00"*31)
13071307
self.assertRaises(binascii.Error, binascii.a2b_uu, b"\xff\x00")
13081308
self.assertRaises(binascii.Error, binascii.a2b_uu, b"!!!!")
1309+
self.assertRaises(binascii.Error, binascii.a2b_uu,
1310+
self.type2test(b""))
1311+
self.assertRaises(binascii.Error, binascii.a2b_uu,
1312+
self.type2test(b"#86)C")[:0])
13091313
self.assertRaises(binascii.Error, binascii.b2a_uu, 46*b"!")
13101314

13111315
# Issue #7701 (crash on a pydebug build)
@@ -1522,6 +1526,9 @@ def test_empty_string(self):
15221526
binascii.crc_hqx(empty, 0)
15231527
continue
15241528
f = getattr(binascii, func)
1529+
if func == 'a2b_uu':
1530+
self.assertRaises(binascii.Error, f, empty)
1531+
continue
15251532
try:
15261533
f(empty)
15271534
except Exception as err:
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Fix an out-of-bounds read of one byte in :func:`binascii.a2b_uu`. Raise
2+
:exc:`binascii.Error`, instead of reading past the buffer end.

Modules/binascii.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,14 @@ binascii_a2b_uu_impl(PyObject *module, Py_buffer *data)
508508
assert(ascii_len >= 0);
509509

510510
/* First byte: binary data length (in bytes) */
511+
if (ascii_len == 0) {
512+
state = get_binascii_state(module);
513+
if (state == NULL) {
514+
return NULL;
515+
}
516+
PyErr_SetString(state->Error, "Missing length byte");
517+
return NULL;
518+
}
511519
bin_len = (*ascii_data++ - ' ') & 077;
512520
ascii_len--;
513521

0 commit comments

Comments
 (0)