summaryrefslogtreecommitdiffstats
path: root/Python
diff options
context:
space:
mode:
authorBrett Cannon <bcannon@gmail.com>2007-10-20 03:43:15 (GMT)
committerBrett Cannon <bcannon@gmail.com>2007-10-20 03:43:15 (GMT)
commit3bb42d9341964fdf094c89c9a64965bd18588476 (patch)
tree4639672babfa984c723e419353f75df144640b88 /Python
parentd5ec98c7fbe1244aa9622e2ecabfb150ce128a62 (diff)
downloadcpython-3bb42d9341964fdf094c89c9a64965bd18588476.zip
cpython-3bb42d9341964fdf094c89c9a64965bd18588476.tar.gz
cpython-3bb42d9341964fdf094c89c9a64965bd18588476.tar.bz2
Fix a memory leak caused by PyTokenizer_FindEncoding() returning a char * that
was PyMem_MALLOC'ed.
Diffstat (limited to 'Python')
-rw-r--r--Python/import.c11
1 files changed, 8 insertions, 3 deletions
diff --git a/Python/import.c b/Python/import.c
index 323b55a..2a316ca 100644
--- a/Python/import.c
+++ b/Python/import.c
@@ -2561,6 +2561,7 @@ call_find_module(char *name, PyObject *path)
struct filedescr *fdp;
char pathname[MAXPATHLEN+1];
FILE *fp = NULL;
+ char *found_encoding = NULL;
char *encoding = NULL;
pathname[0] = '\0';
@@ -2571,15 +2572,17 @@ call_find_module(char *name, PyObject *path)
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 @@ call_find_module(char *name, PyObject *path)
ret = Py_BuildValue("Os(ssi)",
fob, pathname, fdp->suffix, fdp->mode, fdp->type);
Py_DECREF(fob);
+ PyMem_FREE(found_encoding);
+
return ret;
}