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 4 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.
13 changes: 9 additions & 4 deletions Parser/string_parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -168,12 +168,13 @@ 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;
}

size_t len;
int quote = Py_CHARMASK(*s);
int fmode = 0;
*bytesmode = 0;
Expand All @@ -184,17 +185,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,7 +225,7 @@ _PyPegen_parsestr(Parser *p, int *bytesmode, int *rawmode, PyObject **result,
}
/* Skip the leading quote char. */
s++;
len = strlen(s);
len--;
if (len > INT_MAX) {
PyErr_SetString(PyExc_OverflowError, "string to parse is too long");
return -1;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe move this check to the top? I really don't care about the case where the string including quotes is just over INT_MAX but after stripping quotes it fits.

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can do. Also, I just realized I copied the wrong issue number in the title (I meant #97556): is it enough to change the title, or should I create a whole new pull request?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just edit the title.

Expand Down
0