[Python-checkins] python/dist/src/Modules collectionsmodule.c, 1.32, 1.33

rhettinger at users.sourceforge.net rhettinger at users.sourceforge.net
Wed Oct 6 19:51:56 CEST 2004


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

Modified Files:
	collectionsmodule.c 
Log Message:
Armin's patch to prevent overflows.

Index: collectionsmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/collectionsmodule.c,v
retrieving revision 1.32
retrieving revision 1.33
diff -u -d -r1.32 -r1.33
--- collectionsmodule.c	2 Oct 2004 13:59:33 -0000	1.32
+++ collectionsmodule.c	6 Oct 2004 17:51:54 -0000	1.33
@@ -52,8 +52,21 @@
 } block;
 
 static block *
-newblock(block *leftlink, block *rightlink) {
-	block *b = PyMem_Malloc(sizeof(block));
+newblock(block *leftlink, block *rightlink, int len) {
+	block *b;
+	/* To prevent len from overflowing INT_MAX on 64-bit machines, we
+	 * refuse to allocate new blocks if the current len is dangerously
+	 * close.  There is some extra margin to prevent spurious arithmetic
+	 * overflows at various places.  The following check ensures that
+	 * the blocks allocated to the deque, in the worst case, can only
+	 * have INT_MAX-2 entries in total.
+	 */
+	if (len >= INT_MAX - 2*BLOCKLEN) {
+		PyErr_SetString(PyExc_OverflowError,
+				"cannot add more blocks to the deque");
+		return NULL;
+	}
+	b = PyMem_Malloc(sizeof(block));
 	if (b == NULL) {
 		PyErr_NoMemory();
 		return NULL;
@@ -87,7 +100,7 @@
 	if (deque == NULL)
 		return NULL;
 
-	b = newblock(NULL, NULL);
+	b = newblock(NULL, NULL, 0);
 	if (b == NULL) {
 		Py_DECREF(deque);
 		return NULL;
@@ -110,7 +123,7 @@
 {
 	deque->state++;
 	if (deque->rightindex == BLOCKLEN-1) {
-		block *b = newblock(deque->rightblock, NULL);
+		block *b = newblock(deque->rightblock, NULL, deque->len);
 		if (b == NULL)
 			return NULL;
 		assert(deque->rightblock->rightlink == NULL);
@@ -132,7 +145,7 @@
 {
 	deque->state++;
 	if (deque->leftindex == 0) {
-		block *b = newblock(NULL, deque->leftblock);
+		block *b = newblock(NULL, deque->leftblock, deque->len);
 		if (b == NULL)
 			return NULL;
 		assert(deque->leftblock->leftlink == NULL);
@@ -235,7 +248,8 @@
 	while ((item = PyIter_Next(it)) != NULL) {
 		deque->state++;
 		if (deque->rightindex == BLOCKLEN-1) {
-			block *b = newblock(deque->rightblock, NULL);
+			block *b = newblock(deque->rightblock, NULL,
+					    deque->len);
 			if (b == NULL) {
 				Py_DECREF(item);
 				Py_DECREF(it);
@@ -271,7 +285,8 @@
 	while ((item = PyIter_Next(it)) != NULL) {
 		deque->state++;
 		if (deque->leftindex == 0) {
-			block *b = newblock(NULL, deque->leftblock);
+			block *b = newblock(NULL, deque->leftblock,
+					    deque->len);
 			if (b == NULL) {
 				Py_DECREF(item);
 				Py_DECREF(it);



More information about the Python-checkins mailing list