Skip to content

Commit 113038f

Browse files
gh-148016: Improve "Leading padding not allowed" error in Base32 and Base64 decoders (GH-148017)
It is now raised instead of "Excess padding not allowed" if all characters preceding "=" were ignored.
1 parent 985216c commit 113038f

File tree

2 files changed

+10
-6
lines changed

2 files changed

+10
-6
lines changed

Lib/test/test_binascii.py

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -171,8 +171,8 @@ def _assertRegexTemplate(assert_regex, data,
171171
self.assertEqual(binascii.a2b_base64(data),
172172
non_strict_mode_expected_result)
173173

174-
def assertLeadingPadding(*args):
175-
_assertRegexTemplate(r'(?i)Leading padding', *args)
174+
def assertLeadingPadding(*args, **kwargs):
175+
_assertRegexTemplate(r'(?i)Leading padding', *args, **kwargs)
176176

177177
def assertDiscontinuousPadding(*args):
178178
_assertRegexTemplate(r'(?i)Discontinuous padding', *args)
@@ -203,6 +203,7 @@ def assertInvalidLength(data, *args, length=None, **kwargs):
203203
assertLeadingPadding(b'===abcd', b'i\xb7\x1d')
204204
assertLeadingPadding(b'====abcd', b'i\xb7\x1d')
205205
assertLeadingPadding(b'=====abcd', b'i\xb7\x1d')
206+
assertLeadingPadding(b' =abcd', b'i\xb7\x1d', ignorechars=b' ')
206207

207208
assertInvalidLength(b'a=b==', b'i')
208209
assertInvalidLength(b'a=bc=', b'i\xb7')
@@ -785,8 +786,8 @@ def assertExcessData(*args):
785786
def assertExcessPadding(*args):
786787
_assertRegexTemplate(r"(?i)Excess padding", *args)
787788

788-
def assertLeadingPadding(*args):
789-
_assertRegexTemplate(r"(?i)Leading padding", *args)
789+
def assertLeadingPadding(*args, **kwargs):
790+
_assertRegexTemplate(r"(?i)Leading padding", *args, **kwargs)
790791

791792
def assertIncorrectPadding(*args):
792793
_assertRegexTemplate(r"(?i)Incorrect padding", *args)
@@ -853,6 +854,7 @@ def assertInvalidLength(data, *args, length=None, **kwargs):
853854
assertLeadingPadding(b"=======BEEFCAKE", b"\t\x08Q\x01D")
854855
assertLeadingPadding(b"========BEEFCAKE", b"\t\x08Q\x01D")
855856
assertLeadingPadding(b"=========BEEFCAKE", b"\t\x08Q\x01D")
857+
assertLeadingPadding(b" =BEEFCAKE", ignorechars=b' ')
856858

857859
assertIncorrectPadding(b"AB")
858860
assertIncorrectPadding(b"ABCD")

Modules/binascii.c

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -816,8 +816,9 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data, int strict_mode,
816816
}
817817
state = get_binascii_state(module);
818818
if (state) {
819+
unsigned char *bin_data_start = PyBytesWriter_GetData(writer);
819820
PyErr_SetString(state->Error,
820-
(quad_pos == 0 && ascii_data == data->buf)
821+
(quad_pos == 0 && bin_data == bin_data_start)
821822
? "Leading padding not allowed"
822823
: "Excess padding not allowed");
823824
}
@@ -1601,8 +1602,9 @@ binascii_a2b_base32_impl(PyObject *module, Py_buffer *data,
16011602
}
16021603
state = get_binascii_state(module);
16031604
if (state) {
1605+
unsigned char *bin_data_start = PyBytesWriter_GetData(writer);
16041606
PyErr_SetString(state->Error,
1605-
(octa_pos == 0 && ascii_data == data->buf)
1607+
(octa_pos == 0 && bin_data == bin_data_start)
16061608
? "Leading padding not allowed"
16071609
: "Excess padding not allowed");
16081610
}

0 commit comments

Comments
 (0)