Skip to content

Commit 7ae662c

Browse files
committed
String parser no longer crashes on null characters
1 parent db39050 commit 7ae662c

2 files changed

Lines changed: 9 additions & 4 deletions

File tree

‎Lib/test/test_null.py‎

36 Bytes
Binary file not shown.

‎Parser/string_parser.c‎

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -168,12 +168,13 @@ int
168168
_PyPegen_parsestr(Parser *p, int *bytesmode, int *rawmode, PyObject **result,
169169
const char **fstr, Py_ssize_t *fstrlen, Token *t)
170170
{
171-
const char *s = PyBytes_AsString(t->bytes);
172-
if (s == NULL) {
171+
char *s;
172+
Py_ssize_t len;
173+
174+
if (PyBytes_AsStringAndSize(t->bytes, &s, &len)) {
173175
return -1;
174176
}
175177

176-
size_t len;
177178
int quote = Py_CHARMASK(*s);
178179
int fmode = 0;
179180
*bytesmode = 0;
@@ -184,17 +185,21 @@ _PyPegen_parsestr(Parser *p, int *bytesmode, int *rawmode, PyObject **result,
184185
while (!*bytesmode || !*rawmode) {
185186
if (quote == 'b' || quote == 'B') {
186187
quote =(unsigned char)*++s;
188+
len--;
187189
*bytesmode = 1;
188190
}
189191
else if (quote == 'u' || quote == 'U') {
190192
quote = (unsigned char)*++s;
193+
len--;
191194
}
192195
else if (quote == 'r' || quote == 'R') {
193196
quote = (unsigned char)*++s;
197+
len--;
194198
*rawmode = 1;
195199
}
196200
else if (quote == 'f' || quote == 'F') {
197201
quote = (unsigned char)*++s;
202+
len--;
198203
fmode = 1;
199204
}
200205
else {
@@ -220,7 +225,7 @@ _PyPegen_parsestr(Parser *p, int *bytesmode, int *rawmode, PyObject **result,
220225
}
221226
/* Skip the leading quote char. */
222227
s++;
223-
len = strlen(s);
228+
len--;
224229
if (len > INT_MAX) {
225230
PyErr_SetString(PyExc_OverflowError, "string to parse is too long");
226231
return -1;

0 commit comments

Comments
 (0)