Skip to content

Commit 9e1c192

Browse files
committed
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.)
1 parent 450bd87 commit 9e1c192

2 files changed

Lines changed: 12 additions & 1 deletion

File tree

Lib/test/test_binascii.py

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,10 @@ def addnoise(line):
6969
res = res + b
7070
verify(res == testdata)
7171

72+
# Test base64 with just invalid characters, which should return
73+
# empty strings. TBD: shouldn't it raise an exception instead ?
74+
verify(binascii.a2b_base64(fillers) == '')
75+
7276
# Test uu
7377
print "uu test"
7478
MAX_UU = 45

Modules/binascii.c

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -408,9 +408,16 @@ binascii_a2b_base64(PyObject *self, PyObject *args)
408408
return NULL;
409409
}
410410

411-
/* and set string size correctly */
411+
/* And set string size correctly. If the result string is empty
412+
** (because the input was all invalid) return the shared empty
413+
** string instead; _PyString_Resize() won't do this for us.
414+
*/
412415
if (bin_len > 0)
413416
_PyString_Resize(&rv, bin_len);
417+
else {
418+
Py_DECREF(rv);
419+
rv = PyString_FromString("");
420+
}
414421
return rv;
415422
}
416423

0 commit comments

Comments
 (0)