[Python-checkins] CVS: python/dist/src/Modules selectmodule.c,2.57,2.58

Guido van Rossum gvanrossum@users.sourceforge.net
Thu, 25 Oct 2001 13:17:59 -0700


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

Modified Files:
	selectmodule.c 
Log Message:
Fix SF bug #474538: Memory (reference) leak in poller.register (Dave Brueck)

Replace some tortuous code that was trying to be clever but forgot to
DECREF the key and value, by more longwinded but obviously correct
code.

(Inspired by but not copying the fix from SF patch #475033.)


Index: selectmodule.c
===================================================================
RCS file: /cvsroot/python/python/dist/src/Modules/selectmodule.c,v
retrieving revision 2.57
retrieving revision 2.58
diff -C2 -d -r2.57 -r2.58
*** selectmodule.c	2001/10/24 20:37:10	2.57
--- selectmodule.c	2001/10/25 20:17:57	2.58
***************
*** 367,370 ****
--- 367,371 ----
  	PyObject *o, *key, *value;
  	int fd, events = POLLIN | POLLPRI | POLLOUT;
+ 	int err;
  
  	if (!PyArg_ParseTuple(args, "O|i:register", &o, &events)) {
***************
*** 377,385 ****
  	/* Add entry to the internal dictionary: the key is the 
  	   file descriptor, and the value is the event mask. */
! 	if ( (NULL == (key = PyInt_FromLong(fd))) ||
! 	     (NULL == (value = PyInt_FromLong(events))) ||
! 	     (PyDict_SetItem(self->dict, key, value)) == -1) {
  		return NULL;
  	}
  	self->ufd_uptodate = 0;
  		       
--- 378,395 ----
  	/* Add entry to the internal dictionary: the key is the 
  	   file descriptor, and the value is the event mask. */
! 	key = PyInt_FromLong(fd);
! 	if (key == NULL)
! 		return NULL;
! 	value = PyInt_FromLong(events);
! 	if (value == NULL) {
! 		Py_DECREF(key);
  		return NULL;
  	}
+ 	err = PyDict_SetItem(self->dict, key, value);
+ 	Py_DECREF(key);
+ 	Py_DECREF(value);
+ 	if (err < 0)
+ 		return NULL;
+ 
  	self->ufd_uptodate = 0;