From bf84156dcab516350d23801f44246369cc9a671b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Maurycy=20Paw=C5=82owski-Wiero=C5=84ski?= Date: Mon, 4 May 2026 11:40:52 +0200 Subject: [PATCH] gh-148093: Raise binascii.Error from binascii.a2b_uu() on empty input (GH-149077) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Instead of reading past the end of the empty buffer. (cherry picked from commit 0c6d2f64c0c83e7652760f770ff0c5cdc5040426) Co-authored-by: Maurycy Pawłowski-Wieroński --- Lib/test/test_binascii.py | 7 +++++++ .../2026-04-27-22-34-09.gh-issue-148093.9pWceM.rst | 2 ++ Modules/binascii.c | 8 ++++++++ 3 files changed, 17 insertions(+) create mode 100644 Misc/NEWS.d/next/Library/2026-04-27-22-34-09.gh-issue-148093.9pWceM.rst diff --git a/Lib/test/test_binascii.py b/Lib/test/test_binascii.py index 6e75d7d8f28f93..6e397df13c406c 100644 --- a/Lib/test/test_binascii.py +++ b/Lib/test/test_binascii.py @@ -240,6 +240,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, + self.type2test(b"#86)C")[:0]) self.assertRaises(binascii.Error, binascii.b2a_uu, 46*b"!") # Issue #7701 (crash on a pydebug build) @@ -447,6 +451,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: diff --git a/Misc/NEWS.d/next/Library/2026-04-27-22-34-09.gh-issue-148093.9pWceM.rst b/Misc/NEWS.d/next/Library/2026-04-27-22-34-09.gh-issue-148093.9pWceM.rst new file mode 100644 index 00000000000000..9418044201f8bd --- /dev/null +++ b/Misc/NEWS.d/next/Library/2026-04-27-22-34-09.gh-issue-148093.9pWceM.rst @@ -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. diff --git a/Modules/binascii.c b/Modules/binascii.c index 1030eb15f4169c..6b762b809b5989 100644 --- a/Modules/binascii.c +++ b/Modules/binascii.c @@ -219,6 +219,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--;