[Python-checkins] python/dist/src/Modules binascii.c,2.42,2.43

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Tue Sep 7 00:57:28 CEST 2004


Update of /cvsroot/python/python/dist/src/Modules
In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv8167

Modified Files:
	binascii.c 
Log Message:
SF #1022953:  binascii.a2b_hqx("") raises SystemError

Several functions adopted the strategy of altering a full lengthed
string copy and resizing afterwards.  That would fail if the initial
string was short enough (0 or 1) to be interned.  Interning precluded
the subsequent resizing operation.

The solution was to make sure the initial string was at least two
characters long.

Added tests to verify that all binascii functions do not crater when
given an empty string argument.



Index: binascii.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/binascii.c,v
retrieving revision 2.42
retrieving revision 2.43
diff -u -d -r2.42 -r2.43
--- binascii.c	27 Jul 2004 15:03:53 -0000	2.42
+++ binascii.c	6 Sep 2004 22:57:26 -0000	2.43
@@ -276,7 +276,7 @@
 	}
 
 	/* We're lazy and allocate to much (fixed up later) */
-	if ( (rv=PyString_FromStringAndSize(NULL, bin_len*2)) == NULL )
+	if ( (rv=PyString_FromStringAndSize(NULL, bin_len*2+2)) == NULL )
 		return NULL;
 	ascii_data = (unsigned char *)PyString_AsString(rv);
 
@@ -491,8 +491,10 @@
 	if ( !PyArg_ParseTuple(args, "t#:a2b_hqx", &ascii_data, &len) )
 		return NULL;
 
-	/* Allocate a string that is too big (fixed later) */
-	if ( (rv=PyString_FromStringAndSize(NULL, len)) == NULL )
+	/* Allocate a string that is too big (fixed later) 
+	   Add two to the initial length to prevent interning which
+	   would preclude subsequent resizing.  */
+	if ( (rv=PyString_FromStringAndSize(NULL, len+2)) == NULL )
 		return NULL;
 	bin_data = (unsigned char *)PyString_AsString(rv);
 
@@ -528,6 +530,15 @@
 		Py_DECREF(rv);
 		return NULL;
 	}
+
+
+	assert(PyString_Check(rv));
+	assert((bin_data - (unsigned char *)PyString_AsString(rv)) >= 0);
+	assert(!PyString_CHECK_INTERNED(rv));
+	
+	assert(rv->ob_refcnt == 1);
+
+
 	_PyString_Resize(
 		&rv, (bin_data - (unsigned char *)PyString_AsString(rv)));
 	if (rv) {
@@ -553,7 +564,7 @@
 		return NULL;
 
 	/* Worst case: output is twice as big as input (fixed later) */
-	if ( (rv=PyString_FromStringAndSize(NULL, len*2)) == NULL )
+	if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL )
 		return NULL;
 	out_data = (unsigned char *)PyString_AsString(rv);
 
@@ -602,7 +613,7 @@
 		return NULL;
 
 	/* Allocate a buffer that is at least large enough */
-	if ( (rv=PyString_FromStringAndSize(NULL, len*2)) == NULL )
+	if ( (rv=PyString_FromStringAndSize(NULL, len*2+2)) == NULL )
 		return NULL;
 	ascii_data = (unsigned char *)PyString_AsString(rv);
 



More information about the Python-checkins mailing list