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

Skip to content

Commit 1b933ed

Browse files
committed
#3773: Check for errors around the use of PyTokenizer_FindEncoding().
reviewed by Brett Cannon.
1 parent 1d6a16b commit 1b933ed

3 files changed

Lines changed: 9 additions & 1 deletion

File tree

Misc/NEWS

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@ What's New in Python 3.0 release candidate 1
1212
Core and Builtins
1313
-----------------
1414

15+
- Issue 3774: Added a few more checks in PyTokenizer_FindEncoding to handle
16+
error conditions.
17+
1518
- Issue 3594: Fix Parser/tokenizer.c:fp_setreadl() to open the file being
1619
tokenized by either a file path or file pointer for the benefit of
1720
PyTokenizer_FindEncoding().

Parser/tokenizer.c

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1610,7 +1610,10 @@ PyTokenizer_FindEncoding(int fd)
16101610
fclose(fp);
16111611
if (tok->encoding) {
16121612
encoding = (char *)PyMem_MALLOC(strlen(tok->encoding) + 1);
1613-
strcpy(encoding, tok->encoding);
1613+
if (encoding)
1614+
strcpy(encoding, tok->encoding);
1615+
else
1616+
PyErr_NoMemory();
16141617
}
16151618
PyTokenizer_Free(tok);
16161619
return encoding;

Python/import.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2830,6 +2830,8 @@ call_find_module(char *name, PyObject *path)
28302830
memory. */
28312831
found_encoding = PyTokenizer_FindEncoding(fd);
28322832
lseek(fd, 0, 0); /* Reset position */
2833+
if (found_encoding == NULL && PyErr_Occurred())
2834+
return NULL;
28332835
encoding = (found_encoding != NULL) ? found_encoding :
28342836
(char*)PyUnicode_GetDefaultEncoding();
28352837
}

0 commit comments

Comments
 (0)