Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
21 changes: 21 additions & 0 deletions Lib/test/test_ast/test_ast.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,27 @@ def test_compilation_of_ast_nodes_with_default_end_position_values(self):
# Check that compilation doesn't crash. Note: this may crash explicitly only on debug mode.
compile(tree, "<string>", "exec")

def test_compilation_of_ast_nodes_with_negative_position_values(self):
# See https://github.com/python/cpython/issues/130775
alias = ast.alias(name='traceback', lineno=0, col_offset=0)
for attrs in (
{'lineno': -2, 'col_offset': 0},
{'lineno': 0, 'col_offset': -2},
{'lineno': 0, 'col_offset': -2, 'end_col_offset': -2},
{'lineno': -2, 'end_lineno': -2, 'col_offset': 0},
Comment on lines +211 to +214
Copy link
Member

Choose a reason for hiding this comment

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

  • Off topic: are we already testing that passing a non-int is fine?
  • Maybe test with values that are excessively large (either in + or -) just for overflow checks.

Copy link
Member Author

Choose a reason for hiding this comment

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

I will open a new issue for that, if we don't. Thanks for the idea.

):
with self.subTest(attrs=attrs):
tree = ast.Module(body=[
ast.Import(names=[alias], **attrs)
], type_ignores=[])

# It used to crash on this step:
with self.assertRaisesRegex(
ValueError,
'AST node has invalid location',
):
compile(tree, "<string>", "exec")

def test_slice(self):
slc = ast.parse("x[::]").body[0].value.slice
self.assertIsNone(slc.upper)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Validate negative :mod:`ast` locations, we only allow ``-1`` as a special
value.
1 change: 1 addition & 0 deletions Python/assemble.c
Original file line number Diff line number Diff line change
Expand Up @@ -297,6 +297,7 @@ write_location_info_entry(struct assembler* a, location loc, int isize)
int line_delta = loc.lineno - a->a_lineno;
int column = loc.col_offset;
int end_column = loc.end_col_offset;
// Values are validated in `VALIDATE_POSITIONS` in `ast.c`:
assert(column >= -1);
assert(end_column >= -1);
if (column < 0 || end_column < 0) {
Expand Down
8 changes: 8 additions & 0 deletions Python/ast.c
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,14 @@ static int validate_typeparam(type_param_ty);
node->col_offset, node->end_col_offset, node->lineno, node->end_lineno); \
return 0; \
} \
if (node->lineno < -1 || node->end_lineno < -1 || \
node->col_offset < -1 || node->end_col_offset < -1) { \
PyErr_Format(PyExc_ValueError, \
"AST node has invalid location (%d, %d, %d, %d)", \
node->lineno, node->end_lineno, \
node->col_offset, node->end_col_offset); \
return 0; \
} \
if (node->lineno == node->end_lineno && node->col_offset > node->end_col_offset) { \
PyErr_Format(PyExc_ValueError, \
"line %d, column %d-%d is not a valid range", \
Expand Down
Loading