Skip to content

Commit 023ecbd

Browse files
[3.13] gh-148093: Raise binascii.Error from binascii.a2b_uu() on empty input (GH-149077) (GH-149349)
Instead of reading past the end of the empty buffer. (cherry picked from commit 0c6d2f6) Co-authored-by: Maurycy Pawłowski-Wieroński <maurycy@maurycy.com>
1 parent d82d5c2 commit 023ecbd

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
@@ -240,6 +240,10 @@ def test_uu(self):
240240
self.assertEqual(binascii.a2b_uu(b"\xff"), b"\x00"*31)
241241
self.assertRaises(binascii.Error, binascii.a2b_uu, b"\xff\x00")
242242
self.assertRaises(binascii.Error, binascii.a2b_uu, b"!!!!")
243+
self.assertRaises(binascii.Error, binascii.a2b_uu,
244+
self.type2test(b""))
245+
self.assertRaises(binascii.Error, binascii.a2b_uu,
246+
self.type2test(b"#86)C")[:0])
243247
self.assertRaises(binascii.Error, binascii.b2a_uu, 46*b"!")
244248

245249
# Issue #7701 (crash on a pydebug build)
@@ -447,6 +451,9 @@ def test_empty_string(self):
447451
binascii.crc_hqx(empty, 0)
448452
continue
449453
f = getattr(binascii, func)
454+
if func == 'a2b_uu':
455+
self.assertRaises(binascii.Error, f, empty)
456+
continue
450457
try:
451458
f(empty)
452459
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
@@ -219,6 +219,14 @@ binascii_a2b_uu_impl(PyObject *module, Py_buffer *data)
219219
assert(ascii_len >= 0);
220220

221221
/* First byte: binary data length (in bytes) */
222+
if (ascii_len == 0) {
223+
state = get_binascii_state(module);
224+
if (state == NULL) {
225+
return NULL;
226+
}
227+
PyErr_SetString(state->Error, "Missing length byte");
228+
return NULL;
229+
}
222230
bin_len = (*ascii_data++ - ' ') & 077;
223231
ascii_len--;
224232

0 commit comments

Comments
 (0)