Commit 9e1c1925 authored by Thomas Wouters's avatar Thomas Wouters

binascii_a2b_base64: Properly return an empty string if the input was all

    invalid, rather than returning a string of random garbage of the
    estimated result length. Closes SF patch #703471 by Hye-Shik Chang.

Will backport to 2.2-maint (consider it done.)
parent 450bd873
...@@ -69,6 +69,10 @@ for line in map(addnoise, lines): ...@@ -69,6 +69,10 @@ for line in map(addnoise, lines):
res = res + b res = res + b
verify(res == testdata) verify(res == testdata)
# Test base64 with just invalid characters, which should return
# empty strings. TBD: shouldn't it raise an exception instead ?
verify(binascii.a2b_base64(fillers) == '')
# Test uu # Test uu
print "uu test" print "uu test"
MAX_UU = 45 MAX_UU = 45
......
...@@ -408,9 +408,16 @@ binascii_a2b_base64(PyObject *self, PyObject *args) ...@@ -408,9 +408,16 @@ binascii_a2b_base64(PyObject *self, PyObject *args)
return NULL; return NULL;
} }
/* and set string size correctly */ /* And set string size correctly. If the result string is empty
** (because the input was all invalid) return the shared empty
** string instead; _PyString_Resize() won't do this for us.
*/
if (bin_len > 0) if (bin_len > 0)
_PyString_Resize(&rv, bin_len); _PyString_Resize(&rv, bin_len);
else {
Py_DECREF(rv);
rv = PyString_FromString("");
}
return rv; return rv;
} }
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment