Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 1fbb577

Browse files
committed
SF bug #494738: binascii_b2a_base64 overwrites memory.
binascii_b2a_base64(): We didn't allocate enough buffer space for very short inputs (e.g., a 1-byte input can produce a 5-byte output, but we only allocated 2 bytes). I expect that malloc overheads absorbed the overrun in practice, but computing a correct upper bound is a very simple change.
1 parent b6d14da commit 1fbb577

2 files changed

Lines changed: 6 additions & 3 deletions

File tree

Misc/ACKS

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ Benjamin Collar
9292
Jeffery Collins
9393
Matt Conway
9494
David M. Cooke
95+
David Costanzo
9596
Scott Cotton
9697
Greg Couch
9798
Steve Cousins

Modules/binascii.c

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ static char table_a2b_base64[] = {
137137
#define BASE64_PAD '='
138138

139139
/* Max binary chunk size; limited only by available memory */
140-
#define BASE64_MAXBIN (INT_MAX/2 - sizeof(PyStringObject))
140+
#define BASE64_MAXBIN (INT_MAX/2 - sizeof(PyStringObject) - 3)
141141

142142
static unsigned char table_b2a_base64[] =
143143
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
@@ -436,8 +436,10 @@ binascii_b2a_base64(PyObject *self, PyObject *args)
436436
return NULL;
437437
}
438438

439-
/* We're lazy and allocate to much (fixed up later) */
440-
if ( (rv=PyString_FromStringAndSize(NULL, bin_len*2)) == NULL )
439+
/* We're lazy and allocate too much (fixed up later).
440+
"+3" leaves room for up to two pad characters and a trailing
441+
newline. Note that 'b' gets encoded as 'Yg==\n' (1 in, 5 out). */
442+
if ( (rv=PyString_FromStringAndSize(NULL, bin_len*2 + 3)) == NULL )
441443
return NULL;
442444
ascii_data = (unsigned char *)PyString_AsString(rv);
443445

0 commit comments

Comments
 (0)