Fix a memory leak caused by PyTokenizer_FindEncoding() returning a char * that

was PyMem_MALLOC'ed.
This commit is contained in:
Brett Cannon 2007-10-20 03:43:15 +00:00
parent d5ec98c7fb
commit 3bb42d9341
1 changed files with 8 additions and 3 deletions

View File

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