8000 gh-97556: Fix crash on null characters in string literal parser by apccurtiss · Pull Request #97577 · python/cpython · GitHub
[go: up one dir, main page]

Skip to content

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

Open
wants to merge 6 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fix interpreter crash when a string literal contains a null character.
22 changes: 14 additions & 8 deletions Parser/string_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)) {
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;
Expand All @@ -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 {
Expand All @@ -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();
Expand Down
0