@@ -22,6 +22,27 @@ static int validate_stmt(struct validator *, stmt_ty);
2222static int validate_expr (struct validator * , expr_ty , expr_context_ty );
2323static int validate_pattern (struct validator * , pattern_ty , int );
2424
25+ #define VALIDATE_POSITIONS (node ) \
26+ if (node->lineno > node->end_lineno) { \
27+ PyErr_Format(PyExc_ValueError, \
28+ "line %d-%d is not a valid range", \
29+ node->lineno, node->end_lineno); \
30+ return 0; \
31+ } \
32+ if ((node->lineno < 0 && node->end_lineno != node->lineno) || \
33+ (node->col_offset < 0 && node->col_offset != node->end_col_offset)) { \
34+ PyErr_Format(PyExc_ValueError, \
35+ "line %d-%d, column %d-%d is not a valid range", \
36+ node->lineno, node->end_lineno, node->col_offset, node->end_col_offset); \
37+ return 0; \
38+ } \
39+ if (node->lineno == node->end_lineno && node->col_offset > node->end_col_offset) { \
40+ PyErr_Format(PyExc_ValueError, \
41+ "line %d, column %d-%d is not a valid range", \
42+ node->lineno, node->col_offset, node->end_col_offset); \
43+ return 0; \
44+ }
45+
2546static int
2647validate_name (PyObject * name )
2748{
@@ -183,6 +204,7 @@ validate_constant(struct validator *state, PyObject *value)
183204static int
184205validate_expr (struct validator * state , expr_ty exp , expr_context_ty ctx )
185206{
207+ VALIDATE_POSITIONS (exp );
186208 int ret = -1 ;
187209 if (++ state -> recursion_depth > state -> recursion_limit ) {
188210 PyErr_SetString (PyExc_RecursionError ,
@@ -505,6 +527,7 @@ validate_capture(PyObject *name)
505527static int
506528validate_pattern (struct validator * state , pattern_ty p , int star_ok )
507529{
530+ VALIDATE_POSITIONS (p );
508531 int ret = -1 ;
509532 if (++ state -> recursion_depth > state -> recursion_limit ) {
510533 PyErr_SetString (PyExc_RecursionError ,
@@ -674,6 +697,7 @@ validate_body(struct validator *state, asdl_stmt_seq *body, const char *owner)
674697static int
675698validate_stmt (struct validator * state , stmt_ty stmt )
676699{
700+ VALIDATE_POSITIONS (stmt );
677701 int ret = -1 ;
678702 Py_ssize_t i ;
679703 if (++ state -> recursion_depth > state -> recursion_limit ) {
0 commit comments