Skip to content

Commit c48f25a

Browse files
committed
bpo-40958: Avoid buffer overflow in the parser when indexing the current line
1 parent 0d3350d commit c48f25a

File tree

1 file changed

+5
-8
lines changed

1 file changed

+5
-8
lines changed

Parser/pegen.c

+5-8
Original file line numberDiff line numberDiff line change
@@ -141,19 +141,19 @@ _create_dummy_identifier(Parser *p)
141141
static inline Py_ssize_t
142142
byte_offset_to_character_offset(PyObject *line, int col_offset)
143143
{
144-
const char *str = PyUnicode_AsUTF8(line);
144+
Py_ssize_t linesize;
145+
const char *str = PyUnicode_AsUTF8AndSize(line, &linesize);
145146
if (!str) {
146147
return 0;
147148
}
149+
if (col_offset > linesize) {
150+
col_offset = (int)linesize;
151+
}
148152
PyObject *text = PyUnicode_DecodeUTF8(str, col_offset, "replace");
149153
if (!text) {
150154
return 0;
151155
}
152156
Py_ssize_t size = PyUnicode_GET_LENGTH(text);
153-
str = PyUnicode_AsUTF8(text);
154-
if (str != NULL && (int)strlen(str) == col_offset) {
155-
size = strlen(str);
156-
}
157157
Py_DECREF(text);
158158
return size;
159159
}
@@ -400,9 +400,6 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
400400

401401
if (!error_line) {
402402
Py_ssize_t size = p->tok->inp - p->tok->buf;
403-
if (size && p->tok->buf[size-1] == '\n') {
404-
size--;
405-
}
406403
error_line = PyUnicode_DecodeUTF8(p->tok->buf, size, "replace");
407404
if (!error_line) {
408405
goto error;

0 commit comments

Comments
 (0)