From 3e25cd96194e10f6fdc7cb9984004a4caa4c0359 Mon Sep 17 00:00:00 2001 From: "Miss Islington (bot)" <31488909+miss-islington@users.noreply.github.com> Date: Mon, 4 May 2026 12:10:47 +0200 Subject: [PATCH] [3.14] gh-148093: Raise binascii.Error from binascii.a2b_uu() on empty input (GH-149077) (GH-149350) 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 c04ab1e2a5e..82eabf4bb06 100644 --- a/Lib/test/test_binascii.py +++ b/Lib/test/test_binascii.py @@ -240,6 +240,10 @@ class BinASCIITest(unittest.TestCase): 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 @@ class BinASCIITest(unittest.TestCase): 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 00000000000..9418044201f --- /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 1030eb15f41..6b762b809b5 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--;