Skip to content
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

bpo-40958: Avoid buffer overflow in the parser when indexing the current line #20875

Merged
merged 4 commits into from
Jun 16, 2020
Merged
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,2 @@
Fix a possible buffer overflow in the PEG parser when gathering information
for emitting syntax errors. Patch by Pablo Galindo.
20 changes: 9 additions & 11 deletions Parser/pegen.c
Original file line number Diff line number Diff line change
Expand Up @@ -139,21 +139,18 @@ _create_dummy_identifier(Parser *p)
}

static inline Py_ssize_t
byte_offset_to_character_offset(PyObject *line, int col_offset)
byte_offset_to_character_offset(PyObject *line, Py_ssize_t col_offset)
{
const char *str = PyUnicode_AsUTF8(line);
if (!str) {
return 0;
}
assert(col_offset >= 0 && (unsigned long)col_offset <= strlen(str));
PyObject *text = PyUnicode_DecodeUTF8(str, col_offset, "replace");
if (!text) {
return 0;
}
Py_ssize_t size = PyUnicode_GET_LENGTH(text);
str = PyUnicode_AsUTF8(text);
if (str != NULL && (int)strlen(str) == col_offset) {
size = strlen(str);
}
Py_DECREF(text);
return size;
}
Expand Down Expand Up @@ -360,7 +357,7 @@ void *
_PyPegen_raise_error(Parser *p, PyObject *errtype, const char *errmsg, ...)
{
Token *t = p->known_err_token != NULL ? p->known_err_token : p->tokens[p->fill - 1];
int col_offset;
Py_ssize_t col_offset;
if (t->col_offset == -1) {
col_offset = Py_SAFE_DOWNCAST(p->tok->cur - p->tok->buf,
intptr_t, int);
Expand All @@ -380,7 +377,7 @@ _PyPegen_raise_error(Parser *p, PyObject *errtype, const char *errmsg, ...)

void *
_PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
int lineno, int col_offset,
Py_ssize_t lineno, Py_ssize_t col_offset,
pablogsal marked this conversation as resolved.
Show resolved Hide resolved
const char *errmsg, va_list va)
{
PyObject *value = NULL;
Expand All @@ -400,16 +397,17 @@ _PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,

if (!error_line) {
Py_ssize_t size = p->tok->inp - p->tok->buf;
if (size && p->tok->buf[size-1] == '\n') {
size--;
}
error_line = PyUnicode_DecodeUTF8(p->tok->buf, size, "replace");
if (!error_line) {
goto error;
}
}

Py_ssize_t col_number = byte_offset_to_character_offset(error_line, col_offset);
Py_ssize_t col_number = col_offset;
tiran marked this conversation as resolved.
Show resolved Hide resolved

if (p->tok->encoding != NULL) {
pablogsal marked this conversation as resolved.
Show resolved Hide resolved
col_number = byte_offset_to_character_offset(error_line, col_offset);
}

tmp = Py_BuildValue("(OiiN)", p->tok->filename, lineno, col_number, error_line);
if (!tmp) {
Expand Down
4 changes: 2 additions & 2 deletions Parser/pegen.h
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ typedef struct _memo {
typedef struct {
int type;
PyObject *bytes;
int lineno, col_offset, end_lineno, end_col_offset;
Py_ssize_t lineno, col_offset, end_lineno, end_col_offset;
Memo *memo;
} Token;

Expand Down Expand Up @@ -132,7 +132,7 @@ void *_PyPegen_string_token(Parser *p);
const char *_PyPegen_get_expr_name(expr_ty);
void *_PyPegen_raise_error(Parser *p, PyObject *errtype, const char *errmsg, ...);
void *_PyPegen_raise_error_known_location(Parser *p, PyObject *errtype,
int lineno, int col_offset,
Py_ssize_t lineno, Py_ssize_t col_offset,
const char *errmsg, va_list va);
void *_PyPegen_dummy_name(Parser *p, ...);

Expand Down