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

fix: line table overflow #43

Merged
merged 1 commit into from
Oct 13, 2023
Merged
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
27 changes: 18 additions & 9 deletions echion/frame.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,11 +114,14 @@ class Frame
#if PY_VERSION_HEX >= 0x030b0000
// ----------------------------------------------------------------------------
static inline int
_read_varint(unsigned char *table, ssize_t *i)
_read_varint(unsigned char *table, ssize_t size, ssize_t *i)
{
if (*i >= size - 1)
return 0;

int val = table[++*i] & 63;
int shift = 0;
while (table[*i] & 64)
while (table[*i] & 64 && *i < size)
{
shift += 6;
val |= (table[++*i] & 63) << shift;
Expand All @@ -128,9 +131,9 @@ _read_varint(unsigned char *table, ssize_t *i)

// ----------------------------------------------------------------------------
static inline int
_read_signed_varint(unsigned char *table, ssize_t *i)
_read_signed_varint(unsigned char *table, ssize_t size, ssize_t *i)
{
int val = _read_varint(table, i);
int val = _read_varint(table, size, i);
return (val & 1) ? -(val >> 1) : (val >> 1);
}
#endif
Expand Down Expand Up @@ -159,17 +162,17 @@ void Frame::infer_location(PyCodeObject *code, int lasti)
break;

case 14: // Long form
lineno += _read_signed_varint(table_data, &i);
lineno += _read_signed_varint(table_data, len, &i);

this->location.line = lineno;
this->location.line_end = lineno + _read_varint(table_data, &i);
this->location.column = _read_varint(table_data, &i);
this->location.column_end = _read_varint(table_data, &i);
this->location.line_end = lineno + _read_varint(table_data, len, &i);
this->location.column = _read_varint(table_data, len, &i);
this->location.column_end = _read_varint(table_data, len, &i);

break;

case 13: // No column data
lineno += _read_signed_varint(table_data, &i);
lineno += _read_signed_varint(table_data, len, &i);

this->location.line = lineno;
this->location.line_end = lineno;
Expand All @@ -180,6 +183,9 @@ void Frame::infer_location(PyCodeObject *code, int lasti)
case 12: // New lineno
case 11:
case 10:
if (i >= len - 2)
throw LocationError();

lineno += code - 10;

this->location.line = lineno;
Expand All @@ -190,6 +196,9 @@ void Frame::infer_location(PyCodeObject *code, int lasti)
break;

default:
if (i >= len - 1)
throw LocationError();

next_byte = table[++i];

this->location.line = lineno;
Expand Down
Loading