summaryrefslogtreecommitdiffstats
path: root/Modules/binascii.c
diff options
context:
space:
mode:
authorTal Einat <taleinat+github@gmail.com>2018-09-28 05:57:22 (GMT)
committerMiss Islington (bot) <31488909+miss-islington@users.noreply.github.com>2018-09-28 05:57:22 (GMT)
commit1fba2ffc37da52c08db51fe4360459990b0311c9 (patch)
tree1e4f16b34025650937e3f9b5285351e2b8de7c59 /Modules/binascii.c
parent9df346bf98069a87de14a3c2f69009d800994c63 (diff)
downloadcpython-1fba2ffc37da52c08db51fe4360459990b0311c9.zip
cpython-1fba2ffc37da52c08db51fe4360459990b0311c9.tar.gz
cpython-1fba2ffc37da52c08db51fe4360459990b0311c9.tar.bz2
bpo-34736: improve error message for invalid length b64decode inputs (GH-9563)
Improvements: 1. Include the number of valid data characters in the error message. 2. Mention "number of data characters" rather than "length". https://bugs.python.org/issue34736
Diffstat (limited to 'Modules/binascii.c')
-rw-r--r--Modules/binascii.c10
1 files changed, 7 insertions, 3 deletions
diff --git a/Modules/binascii.c b/Modules/binascii.c
index 5667018..19589f9 100644
--- a/Modules/binascii.c
+++ b/Modules/binascii.c
@@ -438,6 +438,7 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
{
const unsigned char *ascii_data;
unsigned char *bin_data;
+ unsigned char *bin_data_start;
int leftbits = 0;
unsigned char this_ch;
unsigned int leftchar = 0;
@@ -461,6 +462,7 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
bin_data = _PyBytesWriter_Alloc(&writer, bin_len);
if (bin_data == NULL)
return NULL;
+ bin_data_start = bin_data;
for( ; ascii_len > 0; ascii_len--, ascii_data++) {
this_ch = *ascii_data;
@@ -516,9 +518,11 @@ binascii_a2b_base64_impl(PyObject *module, Py_buffer *data)
** 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");
+ PyErr_Format(Error,
+ "Invalid base64-encoded string: "
+ "number of data characters (%d) cannot be 1 more "
+ "than a multiple of 4",
+ (bin_data - bin_data_start) / 3 * 4 + 1);
} else {
PyErr_SetString(Error, "Incorrect padding");
}