[Python-3000-checkins] r58556 - python/branches/py3k/Python/import.c

brett.cannon python-3000-checkins at python.org
Sat Oct 20 05:43:16 CEST 2007


Author: brett.cannon
Date: Sat Oct 20 05:43:15 2007
New Revision: 58556

Modified:
   python/branches/py3k/Python/import.c
Log:
Fix a memory leak caused by PyTokenizer_FindEncoding() returning a char * that
was PyMem_MALLOC'ed.


Modified: python/branches/py3k/Python/import.c
==============================================================================
--- python/branches/py3k/Python/import.c	(original)
+++ python/branches/py3k/Python/import.c	Sat Oct 20 05:43:15 2007
@@ -2561,6 +2561,7 @@
 	struct filedescr *fdp;
 	char pathname[MAXPATHLEN+1];
 	FILE *fp = NULL;
+	char *found_encoding = NULL;
 	char *encoding = NULL;
 
 	pathname[0] = '\0';
@@ -2571,15 +2572,17 @@
 		return NULL;
 	if (fp != NULL) {
 		if (strchr(fdp->mode, 'b') == NULL) {
-			/* Python text file, get encoding from tokenizer */
-			encoding = PyTokenizer_FindEncoding(fp);
-			encoding = (encoding != NULL) ? encoding :
+			/* PyTokenizer_FindEncoding() returns PyMem_MALLOC'ed
+			   memory. */
+			found_encoding = PyTokenizer_FindEncoding(fp);
+			encoding = (found_encoding != NULL) ? found_encoding :
 				   (char*)PyUnicode_GetDefaultEncoding();
 		}
 		fob = PyFile_FromFileEx(fp, pathname, fdp->mode, fclose, -1,
 					(char*)encoding, NULL);
 		if (fob == NULL) {
 			fclose(fp);
+			PyMem_FREE(found_encoding);
 			return NULL;
 		}
 	}
@@ -2590,6 +2593,8 @@
 	ret = Py_BuildValue("Os(ssi)",
 		      fob, pathname, fdp->suffix, fdp->mode, fdp->type);
 	Py_DECREF(fob);
+	PyMem_FREE(found_encoding);
+
 	return ret;
 }
 


More information about the Python-3000-checkins mailing list