Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions Lib/test/test_binascii.py
Original file line number Diff line number Diff line change
Expand Up @@ -1306,6 +1306,10 @@ def test_uu(self):
self.assertEqual(binascii.a2b_uu(b"\xff"), b"\x00"*31)
self.assertRaises(binascii.Error, binascii.a2b_uu, b"\xff\x00")
self.assertRaises(binascii.Error, binascii.a2b_uu, b"!!!!")
self.assertRaises(binascii.Error, binascii.a2b_uu,
self.type2test(b""))
self.assertRaises(binascii.Error, binascii.a2b_uu,
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do we need this second test case? The [:0] makes it an empty sequence?

Copy link
Copy Markdown
Contributor Author

@maurycy maurycy Apr 27, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@sethmlarson

class MemoryviewBinASCIITest(BinASCIITest):
type2test = memoryview

memoryview(b"#86)C")[:0] is the actual UB case here: zero slice with larger underlying buffer

@serhiy-storchaka came up with it:

#148093 (comment)

Truth to be told, this BinASCIITest inheritance is very confusing and make lots of tests without self.type2test redundant but I didn't want to overhaul it. Do you want me to create a separate issue documenting problems with BinASCIITest?

self.type2test(b"#86)C")[:0])
self.assertRaises(binascii.Error, binascii.b2a_uu, 46*b"!")

# Issue #7701 (crash on a pydebug build)
Expand Down Expand Up @@ -1522,6 +1526,9 @@ def test_empty_string(self):
binascii.crc_hqx(empty, 0)
continue
f = getattr(binascii, func)
if func == 'a2b_uu':
self.assertRaises(binascii.Error, f, empty)
continue
try:
f(empty)
except Exception as err:
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Fix an out-of-bounds read of one byte in :func:`binascii.a2b_uu`. Raise
:exc:`binascii.Error`, instead of reading past the buffer end.
8 changes: 8 additions & 0 deletions Modules/binascii.c
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,14 @@ binascii_a2b_uu_impl(PyObject *module, Py_buffer *data)
assert(ascii_len >= 0);

/* First byte: binary data length (in bytes) */
if (ascii_len == 0) {
state = get_binascii_state(module);
if (state == NULL) {
return NULL;
}
PyErr_SetString(state->Error, "Missing length byte");
return NULL;
}
bin_len = (*ascii_data++ - ' ') & 077;
ascii_len--;

Expand Down
Loading