summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
-rw-r--r--Lib/test/test_binascii.py15
-rw-r--r--Misc/NEWS.d/next/Library/2021-07-19-18-45-00.bpo-44678.YMEAu0.rst1
-rw-r--r--Modules/binascii.c7
3 files changed, 15 insertions, 8 deletions
diff --git a/Lib/test/test_binascii.py b/Lib/test/test_binascii.py
index 74438d8..da8f9d1 100644
--- a/Lib/test/test_binascii.py
+++ b/Lib/test/test_binascii.py
@@ -130,9 +130,12 @@ class BinASCIITest(unittest.TestCase):
def assertNonBase64Data(data, non_strict_mode_expected_result: bytes):
_assertRegexTemplate(r'(?i)Only base64 data', data, non_strict_mode_expected_result)
- def assertMalformedPadding(data, non_strict_mode_expected_result: bytes):
+ def assertLeadingPadding(data, non_strict_mode_expected_result: bytes):
_assertRegexTemplate(r'(?i)Leading padding', data, non_strict_mode_expected_result)
+ def assertDiscontinuousPadding(data, non_strict_mode_expected_result: bytes):
+ _assertRegexTemplate(r'(?i)Discontinuous padding', data, non_strict_mode_expected_result)
+
# Test excess data exceptions
assertExcessData(b'ab==a', b'i')
assertExcessData(b'ab===', b'i')
@@ -148,11 +151,11 @@ class BinASCIITest(unittest.TestCase):
assertNonBase64Data(b'a\x00b==', b'i')
# Test malformed padding
- assertMalformedPadding(b'=', b'')
- assertMalformedPadding(b'==', b'')
- assertMalformedPadding(b'===', b'')
- assertMalformedPadding(b'ab=c=', b'i\xb7')
- assertMalformedPadding(b'ab=ab==', b'i\xb6\x9b')
+ assertLeadingPadding(b'=', b'')
+ assertLeadingPadding(b'==', b'')
+ assertLeadingPadding(b'===', b'')
+ assertDiscontinuousPadding(b'ab=c=', b'i\xb7')
+ assertDiscontinuousPadding(b'ab=ab==', b'i\xb6\x9b')
def test_base64errors(self):
diff --git a/Misc/NEWS.d/next/Library/2021-07-19-18-45-00.bpo-44678.YMEAu0.rst b/Misc/NEWS.d/next/Library/2021-07-19-18-45-00.bpo-44678.YMEAu0.rst
new file mode 100644
index 0000000..991b157
--- /dev/null
+++ b/Misc/NEWS.d/next/Library/2021-07-19-18-45-00.bpo-44678.YMEAu0.rst
@@ -0,0 +1 @@
+Added a separate error message for discontinuous padding in *binascii.a2b_base64* strict mode.
diff --git a/Modules/binascii.c b/Modules/binascii.c
index 50f25b4..e80eb2a 100644
--- a/Modules/binascii.c
+++ b/Modules/binascii.c
@@ -464,7 +464,6 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode)
unsigned char *bin_data_start = bin_data;
if (strict_mode && ascii_len > 0 && ascii_data[0] == '=') {
- malformed_padding:
state = get_binascii_state(module);
if (state) {
PyErr_SetString(state->Error, "Leading padding not allowed");
@@ -516,7 +515,11 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode)
// Characters that are not '=', in the middle of the padding, are not allowed
if (strict_mode && padding_started) {
- goto malformed_padding;
+ state = get_binascii_state(module);
+ if (state) {
+ PyErr_SetString(state->Error, "Discontinuous padding not allowed");
+ }
+ goto error_end;
}
pads = 0;