Repository navigation
gh-97556: Fix crash on null characters in string literal parser #97577
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
7ae662c
fd4161d
a54e0ff
c48ec29
b704f7e
5c9dd40
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1 @@ | ||
| Fix interpreter crash when a string literal contains a null character. | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -168,12 +168,18 @@ int | |
| _PyPegen_parsestr(Parser *p, int *bytesmode, int *rawmode, PyObject **result, | ||
| const char **fstr, Py_ssize_t *fstrlen, Token *t) | ||
| { | ||
| const char *s = PyBytes_AsString(t->bytes); | ||
| if (s == NULL) { | ||
| char *s; | ||
| Py_ssize_t len; | ||
|
|
||
| if (PyBytes_AsStringAndSize(t->bytes, &s, &len)) { | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Shouldn't we reject this in the tokenizer instead? Otherwise files with NUL bytes in a string literal become valid source, while
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is a crash fix but it lands without a regression test. I know a |
||
| return -1; | ||
| } | ||
|
|
||
| if (len > INT_MAX) { | ||
| PyErr_SetString(PyExc_OverflowError, "string to parse is too long"); | ||
| return -1; | ||
| } | ||
|
|
||
| size_t len; | ||
| int quote = Py_CHARMASK(*s); | ||
| int fmode = 0; | ||
| *bytesmode = 0; | ||
|
|
@@ -184,17 +190,21 @@ _PyPegen_parsestr(Parser *p, int *bytesmode, int *rawmode, PyObject **result, | |
| while (!*bytesmode || !*rawmode) { | ||
| if (quote == 'b' || quote == 'B') { | ||
| quote =(unsigned char)*++s; | ||
| len--; | ||
| *bytesmode = 1; | ||
| } | ||
| else if (quote == 'u' || quote == 'U') { | ||
| quote = (unsigned char)*++s; | ||
| len--; | ||
| } | ||
| else if (quote == 'r' || quote == 'R') { | ||
| quote = (unsigned char)*++s; | ||
| len--; | ||
| *rawmode = 1; | ||
| } | ||
| else if (quote == 'f' || quote == 'F') { | ||
| quote = (unsigned char)*++s; | ||
| len--; | ||
| fmode = 1; | ||
| } | ||
| else { | ||
|
|
@@ -220,11 +230,7 @@ _PyPegen_parsestr(Parser *p, int *bytesmode, int *rawmode, PyObject **result, | |
| } | ||
| /* Skip the leading quote char. */ | ||
| s++; | ||
| len = strlen(s); | ||
| if (len > INT_MAX) { | ||
| PyErr_SetString(PyExc_OverflowError, "string to parse is too long"); | ||
| return -1; | ||
| } | ||
| len--; | ||
| if (s[--len] != quote) { | ||
| /* Last quote char must match the first. */ | ||
| PyErr_BadInternalCall(); | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Nit: this NEWS entry is filed under
gh-issue-96670, but this PR fixesgh-97556. Can we rename it so the changelog points to the right issue?