Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 92dc80a

Browse files
committed
Check the return values for all functions returning an ast node.
Failure to do it may result in strange error messages or even crashes, in admittedly convoluted cases that are normally syntax errors, like: def f(*xx, __debug__): pass
1 parent 5c1808a commit 92dc80a

1 file changed

Lines changed: 27 additions & 26 deletions

File tree

Python/ast.c

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -688,6 +688,8 @@ handle_keywordonly_args(struct compiling *c, const node *n, int start,
688688
case tfpdef:
689689
if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
690690
expression = ast_for_expr(c, CHILD(n, i + 2));
691+
if (!expression)
692+
goto error;
691693
asdl_seq_SET(kwdefaults, j, expression);
692694
i += 2; /* '=' and test */
693695
}
@@ -697,10 +699,8 @@ handle_keywordonly_args(struct compiling *c, const node *n, int start,
697699
if (NCH(ch) == 3) {
698700
/* ch is NAME ':' test */
699701
annotation = ast_for_expr(c, CHILD(ch, 2));
700-
if (!annotation) {
701-
ast_error(ch, "expected expression");
702+
if (!annotation)
702703
goto error;
703-
}
704704
}
705705
else {
706706
annotation = NULL;
@@ -794,22 +794,22 @@ ast_for_arguments(struct compiling *c, const node *n)
794794
}
795795
posargs = (nposargs ? asdl_seq_new(nposargs, c->c_arena) : NULL);
796796
if (!posargs && nposargs)
797-
goto error;
797+
return NULL;
798798
kwonlyargs = (nkwonlyargs ?
799799
asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
800800
if (!kwonlyargs && nkwonlyargs)
801-
goto error;
801+
return NULL;
802802
posdefaults = (nposdefaults ?
803803
asdl_seq_new(nposdefaults, c->c_arena) : NULL);
804804
if (!posdefaults && nposdefaults)
805-
goto error;
805+
return NULL;
806806
/* The length of kwonlyargs and kwdefaults are same
807807
since we set NULL as default for keyword only argument w/o default
808808
- we have sequence data structure, but no dictionary */
809809
kwdefaults = (nkwonlyargs ?
810810
asdl_seq_new(nkwonlyargs, c->c_arena) : NULL);
811811
if (!kwdefaults && nkwonlyargs)
812-
goto error;
812+
return NULL;
813813

814814
if (nposargs + nkwonlyargs > 255) {
815815
ast_error(n, "more than 255 arguments");
@@ -833,7 +833,7 @@ ast_for_arguments(struct compiling *c, const node *n)
833833
if (i + 1 < NCH(n) && TYPE(CHILD(n, i + 1)) == EQUAL) {
834834
expr_ty expression = ast_for_expr(c, CHILD(n, i + 2));
835835
if (!expression)
836-
goto error;
836+
return NULL;
837837
assert(posdefaults != NULL);
838838
asdl_seq_SET(posdefaults, j++, expression);
839839
i += 2;
@@ -842,27 +842,27 @@ ast_for_arguments(struct compiling *c, const node *n)
842842
else if (found_default) {
843843
ast_error(n,
844844
"non-default argument follows default argument");
845-
goto error;
845+
return NULL;
846846
}
847847
arg = compiler_arg(c, ch);
848848
if (!arg)
849-
goto error;
849+
return NULL;
850850
asdl_seq_SET(posargs, k++, arg);
851851
i += 2; /* the name and the comma */
852852
break;
853853
case STAR:
854854
if (i+1 >= NCH(n)) {
855855
ast_error(CHILD(n, i),
856856
"named arguments must follow bare *");
857-
goto error;
857+
return NULL;
858858
}
859859
ch = CHILD(n, i+1); /* tfpdef or COMMA */
860860
if (TYPE(ch) == COMMA) {
861861
int res = 0;
862862
i += 2; /* now follows keyword only arguments */
863863
res = handle_keywordonly_args(c, n, i,
864864
kwonlyargs, kwdefaults);
865-
if (res == -1) goto error;
865+
if (res == -1) return NULL;
866866
i = res; /* res has new position to process */
867867
}
868868
else {
@@ -874,14 +874,16 @@ ast_for_arguments(struct compiling *c, const node *n)
874874
if (NCH(ch) > 1) {
875875
/* there is an annotation on the vararg */
876876
varargannotation = ast_for_expr(c, CHILD(ch, 2));
877+
if (!varargannotation)
878+
return NULL;
877879
}
878880
i += 3;
879881
if (i < NCH(n) && (TYPE(CHILD(n, i)) == tfpdef
880882
|| TYPE(CHILD(n, i)) == vfpdef)) {
881883
int res = 0;
882884
res = handle_keywordonly_args(c, n, i,
883885
kwonlyargs, kwdefaults);
884-
if (res == -1) goto error;
886+
if (res == -1) return NULL;
885887
i = res; /* res has new position to process */
886888
}
887889
}
@@ -893,26 +895,24 @@ ast_for_arguments(struct compiling *c, const node *n)
893895
if (NCH(ch) > 1) {
894896
/* there is an annotation on the kwarg */
895897
kwargannotation = ast_for_expr(c, CHILD(ch, 2));
898+
if (!kwargannotation)
899+
return NULL;
896900
}
897901
if (!kwarg)
898-
goto error;
902+
return NULL;
899903
if (forbidden_name(kwarg, CHILD(ch, 0), 0))
900-
goto error;
904+
return NULL;
901905
i += 3;
902906
break;
903907
default:
904908
PyErr_Format(PyExc_SystemError,
905909
"unexpected node in varargslist: %d @ %d",
906910
TYPE(ch), i);
907-
goto error;
911+
return NULL;
908912
}
909913
}
910914
return arguments(posargs, vararg, varargannotation, kwonlyargs, kwarg,
911915
kwargannotation, posdefaults, kwdefaults, c->c_arena);
912-
error:
913-
Py_XDECREF(vararg);
914-
Py_XDECREF(kwarg);
915-
return NULL;
916916
}
917917

918918
static expr_ty
@@ -997,9 +997,9 @@ ast_for_decorators(struct compiling *c, const node *n)
997997

998998
for (i = 0; i < NCH(n); i++) {
999999
d = ast_for_decorator(c, CHILD(n, i));
1000-
if (!d)
1001-
return NULL;
1002-
asdl_seq_SET(decorator_seq, i, d);
1000+
if (!d)
1001+
return NULL;
1002+
asdl_seq_SET(decorator_seq, i, d);
10031003
}
10041004
return decorator_seq;
10051005
}
@@ -1027,7 +1027,7 @@ ast_for_funcdef(struct compiling *c, const node *n, asdl_seq *decorator_seq)
10271027
if (TYPE(CHILD(n, name_i+2)) == RARROW) {
10281028
returns = ast_for_expr(c, CHILD(n, name_i + 3));
10291029
if (!returns)
1030-
return NULL;
1030+
return NULL;
10311031
name_i += 2;
10321032
}
10331033
body = ast_for_suite(c, CHILD(n, name_i + 3));
@@ -2136,11 +2136,10 @@ ast_for_expr_stmt(struct compiling *c, const node *n)
21362136
return NULL;
21372137
}
21382138
e = ast_for_testlist(c, ch);
2139-
2140-
/* set context to assign */
21412139
if (!e)
21422140
return NULL;
21432141

2142+
/* set context to assign */
21442143
if (!set_context(c, e, Store, CHILD(n, i)))
21452144
return NULL;
21462145

@@ -2960,6 +2959,8 @@ ast_for_with_item(struct compiling *c, const node *n, asdl_seq *content)
29602959

29612960
REQ(n, with_item);
29622961
context_expr = ast_for_expr(c, CHILD(n, 0));
2962+
if (!context_expr)
2963+
return NULL;
29632964
if (NCH(n) == 3) {
29642965
optional_vars = ast_for_expr(c, CHILD(n, 2));
29652966

0 commit comments

Comments
 (0)