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
7d1d3e9
15dfa85
883f6f5
d62c4e2
7f37169
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 |
|---|---|---|
|
|
@@ -249,12 +249,18 @@ _PyPegen_decode_string(Parser *p, int raw, const char *s, size_t len, Token *t) | |
| PyObject * | ||
| _PyPegen_parse_string(Parser *p, 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 NULL; | ||
| } | ||
|
|
||
| if (len > INT_MAX) { | ||
| PyErr_SetString(PyExc_OverflowError, "string to parse is too long"); | ||
| return NULL; | ||
| } | ||
|
|
||
| size_t len; | ||
| int quote = Py_CHARMASK(*s); | ||
| int bytesmode = 0; | ||
| int rawmode = 0; | ||
|
|
@@ -263,13 +269,16 @@ _PyPegen_parse_string(Parser *p, Token *t) | |
| 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 { | ||
|
|
@@ -285,15 +294,11 @@ _PyPegen_parse_string(Parser *p, Token *t) | |
|
|
||
| /* Skip the leading quote char. */ | ||
| s++; | ||
| len = strlen(s); | ||
| len--; | ||
| // gh-120155: 's' contains at least the trailing quote, | ||
| // so the code '--len' below is safe. | ||
| assert(len >= 1); | ||
|
|
||
| if (len > INT_MAX) { | ||
| PyErr_SetString(PyExc_OverflowError, "string to parse is too long"); | ||
| return NULL; | ||
| } | ||
| 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?