Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3bb42d9

Browse files
committed
Fix a memory leak caused by PyTokenizer_FindEncoding() returning a char * that
was PyMem_MALLOC'ed.
1 parent d5ec98c commit 3bb42d9

1 file changed

Lines changed: 8 additions & 3 deletions

File tree

Python/import.c

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2561,6 +2561,7 @@ call_find_module(char *name, PyObject *path)
25612561
struct filedescr *fdp;
25622562
char pathname[MAXPATHLEN+1];
25632563
FILE *fp = NULL;
2564+
char *found_encoding = NULL;
25642565
char *encoding = NULL;
25652566

25662567
pathname[0] = '\0';
@@ -2571,15 +2572,17 @@ call_find_module(char *name, PyObject *path)
25712572
return NULL;
25722573
if (fp != NULL) {
25732574
if (strchr(fdp->mode, 'b') == NULL) {
2574-
/* Python text file, get encoding from tokenizer */
2575-
encoding = PyTokenizer_FindEncoding(fp);
2576-
encoding = (encoding != NULL) ? encoding :
2575+
/* PyTokenizer_FindEncoding() returns PyMem_MALLOC'ed
2576+
memory. */
2577+
found_encoding = PyTokenizer_FindEncoding(fp);
2578+
encoding = (found_encoding != NULL) ? found_encoding :
25772579
(char*)PyUnicode_GetDefaultEncoding();
25782580
}
25792581
fob = PyFile_FromFileEx(fp, pathname, fdp->mode, fclose, -1,
25802582
(char*)encoding, NULL);
25812583
if (fob == NULL) {
25822584
fclose(fp);
2585+
PyMem_FREE(found_encoding);
25832586
return NULL;
25842587
}
25852588
}
@@ -2590,6 +2593,8 @@ call_find_module(char *name, PyObject *path)
25902593
ret = Py_BuildValue("Os(ssi)",
25912594
fob, pathname, fdp->suffix, fdp->mode, fdp->type);
25922595
Py_DECREF(fob);
2596+
PyMem_FREE(found_encoding);
2597+
25932598
return ret;
25942599
}
25952600

0 commit comments

Comments
 (0)