bpo-33770: improve base64 exception message for encoded inputs of invalid length (GH-7416) (GH-7602)
(cherry picked from commit 1b85c71a2136d3fa6a1da05b27b1fe4e4b8ee45e)
Co-authored-by: Tal Einat <taleinat+github@gmail.com>
diff --git a/Modules/binascii.c b/Modules/binascii.c
index 1af6b7f..2df80af 100644
--- a/Modules/binascii.c
+++ b/Modules/binascii.c
@@ -510,7 +510,18 @@
}
if (leftbits != 0) {
- PyErr_SetString(Error, "Incorrect padding");
+ if (leftbits == 6) {
+ /*
+ ** There is exactly one extra valid, non-padding, base64 character.
+ ** This is an invalid length, as there is no possible input that
+ ** could encoded into such a base64 string.
+ */
+ PyErr_SetString(Error,
+ "Invalid base64-encoded string: "
+ "length cannot be 1 more than a multiple of 4");
+ } else {
+ PyErr_SetString(Error, "Incorrect padding");
+ }
_PyBytesWriter_Dealloc(&writer);
return NULL;
}