[Python-checkins] CVS: python/dist/src/Objects dictobject.c,2.75,2.76

Guido van Rossum gvanrossum@users.sourceforge.net
Sun, 15 Apr 2001 17:02:35 -0700


Update of /cvsroot/python/python/dist/src/Objects
In directory usw-pr-cvs1:/tmp/cvs-serv31193

Modified Files:
	dictobject.c 
Log Message:
Tim pointed out a remaining vulnerability in popitem(): the
PyTuple_New() could *conceivably* clear the dict, so move the test for
an empty dict after the tuple allocation.  It means that we waste time
allocating and deallocating a 2-tuple when the dict is empty, but who
cares.  It also means that when the dict is empty *and* there's no
memory to allocate a 2-tuple, we raise MemoryError, not KeyError --
but that may actually a good idea: if there's no room for a lousy
2-tuple, what are the chances that there's room for a KeyError
instance?


Index: dictobject.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Objects/dictobject.c,v
retrieving revision 2.75
retrieving revision 2.76
diff -C2 -r2.75 -r2.76
*** dictobject.c	2001/04/15 22:16:26	2.75
--- dictobject.c	2001/04/16 00:02:32	2.76
***************
*** 1156,1164 ****
  	if (!PyArg_NoArgs(args))
  		return NULL;
- 	if (mp->ma_used == 0) {
- 		PyErr_SetString(PyExc_KeyError,
- 				"popitem(): dictionary is empty");
- 		return NULL;
- 	}
  	/* Allocate the result tuple first.  Believe it or not,
  	 * this allocation could trigger a garbage collection which
--- 1156,1159 ----
***************
*** 1170,1173 ****
--- 1165,1174 ----
  	if (res == NULL)
  		return NULL;
+ 	if (mp->ma_used == 0) {
+ 		Py_DECREF(res);
+ 		PyErr_SetString(PyExc_KeyError,
+ 				"popitem(): dictionary is empty");
+ 		return NULL;
+ 	}
  	/* Set ep to "the first" dict entry with a value.  We abuse the hash
  	 * field of slot 0 to hold a search finger: