@@ -22,6 +22,27 @@ static int validate_stmt(struct validator *, stmt_ty);
22
22
static int validate_expr (struct validator * , expr_ty , expr_context_ty );
23
23
static int validate_pattern (struct validator * , pattern_ty , int );
24
24
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
+
25
46
static int
26
47
validate_name (PyObject * name )
27
48
{
@@ -75,6 +96,7 @@ validate_args(struct validator *state, asdl_arg_seq *args)
75
96
Py_ssize_t i ;
76
97
for (i = 0 ; i < asdl_seq_LEN (args ); i ++ ) {
77
98
arg_ty arg = asdl_seq_GET (args , i );
99
+ VALIDATE_POSITIONS (arg );
78
100
if (arg -> annotation && !validate_expr (state , arg -> annotation , Load ))
79
101
return 0 ;
80
102
}
@@ -183,6 +205,7 @@ validate_constant(struct validator *state, PyObject *value)
183
205
static int
184
206
validate_expr (struct validator * state , expr_ty exp , expr_context_ty ctx )
185
207
{
208
+ VALIDATE_POSITIONS (exp );
186
209
int ret = -1 ;
187
210
if (++ state -> recursion_depth > state -> recursion_limit ) {
188
211
PyErr_SetString (PyExc_RecursionError ,
@@ -505,6 +528,7 @@ validate_capture(PyObject *name)
505
528
static int
506
529
validate_pattern (struct validator * state , pattern_ty p , int star_ok )
507
530
{
531
+ VALIDATE_POSITIONS (p );
508
532
int ret = -1 ;
509
533
if (++ state -> recursion_depth > state -> recursion_limit ) {
510
534
PyErr_SetString (PyExc_RecursionError ,
@@ -674,6 +698,7 @@ validate_body(struct validator *state, asdl_stmt_seq *body, const char *owner)
674
698
static int
675
699
validate_stmt (struct validator * state , stmt_ty stmt )
676
700
{
701
+ VALIDATE_POSITIONS (stmt );
677
702
int ret = -1 ;
678
703
Py_ssize_t i ;
679
704
if (++ state -> recursion_depth > state -> recursion_limit ) {
@@ -807,6 +832,7 @@ validate_stmt(struct validator *state, stmt_ty stmt)
807
832
}
808
833
for (i = 0 ; i < asdl_seq_LEN (stmt -> v .Try .handlers ); i ++ ) {
809
834
excepthandler_ty handler = asdl_seq_GET (stmt -> v .Try .handlers , i );
835
+ VALIDATE_POSITIONS (handler );
810
836
if ((handler -> v .ExceptHandler .type &&
811
837
!validate_expr (state , handler -> v .ExceptHandler .type , Load )) ||
812
838
!validate_body (state , handler -> v .ExceptHandler .body , "ExceptHandler" ))
0 commit comments