From 830f5f9031c55ed5433fcf6284f269b5aaf370b4 Mon Sep 17 00:00:00 2001 From: hauntsaninja Date: Thu, 29 Oct 2026 15:44:36 -0700 Subject: [PATCH] Preserve ValueError for invalid base64 input Co-authored by Codex --- mypyc/lib-rt/base64/librt_base64.c | 3 +-- mypyc/test-data/run-base64.test | 9 +++++++++ 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/mypyc/lib-rt/base64/librt_base64.c b/mypyc/lib-rt/base64/librt_base64.c index 6f61b0d9bd419..ba250f846c66b 100644 --- a/mypyc/lib-rt/base64/librt_base64.c +++ b/mypyc/lib-rt/base64/librt_base64.c @@ -284,8 +284,7 @@ b64decode_handle_invalid_input( Py_DECREF(out_bytes); if (ret == 0) { PyErr_SetString(PyExc_ValueError, "Only base64 data is allowed"); - } - if (ret == -1) { + } else if (ret == -1) { PyErr_SetString(PyExc_NotImplementedError, "base64 codec not available in this build"); } else { PyErr_SetString(PyExc_RuntimeError, "base64_decode failed"); diff --git a/mypyc/test-data/run-base64.test b/mypyc/test-data/run-base64.test index 022a0e8c31136..ee2c5000bafeb 100644 --- a/mypyc/test-data/run-base64.test +++ b/mypyc/test-data/run-base64.test @@ -63,6 +63,15 @@ def test_decode_basic() -> None: with assertRaises(ValueError): b64decode(non_ascii) +def test_invalid_input_length() -> None: + for decode in (b64decode, urlsafe_b64decode): + for data in (b"A===", b"AAAAA===", b"???A===!!!"): + with assertRaises(ValueError): + decode(data) + with assertRaises(ValueError): + decode(data.decode("ascii")) + assert decode(b"YQ==") == b"a" + def check_decode(b: bytes, encoded: bool = False) -> None: if encoded: enc = b