Skip to content

Commit 611c8dc

Browse files
committed
ast: Walk - fix null pointer deref and increase test coverage
1 parent 8eb8664 commit 611c8dc

File tree

2 files changed

+78
-5
lines changed

2 files changed

+78
-5
lines changed

ast/walk.go

+15-5
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,9 @@ func Walk(ast Ast, Visit func(Ast) bool) {
7373
// Body []Stmt
7474
// DecoratorList []Expr
7575
// Returns Expr
76-
walk(node.Args)
76+
if node.Args != nil {
77+
walk(node.Args)
78+
}
7779
walkStmts(node.Body)
7880
walkExprs(node.DecoratorList)
7981
walk(node.Returns)
@@ -226,7 +228,9 @@ func Walk(ast Ast, Visit func(Ast) bool) {
226228
case *Lambda:
227229
// Args *Arguments
228230
// Body Expr
229-
walk(node.Args)
231+
if node.Args != nil {
232+
walk(node.Args)
233+
}
230234
walk(node.Body)
231235

232236
case *IfExp:
@@ -385,18 +389,24 @@ func Walk(ast Ast, Visit func(Ast) bool) {
385389
for _, arg := range node.Args {
386390
walk(arg)
387391
}
388-
walk(node.Vararg)
392+
if node.Vararg != nil {
393+
walk(node.Vararg)
394+
}
389395
for _, arg := range node.Kwonlyargs {
390396
walk(arg)
391397
}
392398
walkExprs(node.KwDefaults)
393-
walk(node.Kwarg)
399+
if node.Kwarg != nil {
400+
walk(node.Kwarg)
401+
}
394402
walkExprs(node.Defaults)
395403

396404
case *Arg:
397405
// Arg Identifier
398406
// Annotation Expr
399-
walk(node.Annotation)
407+
if node.Annotation != nil {
408+
walk(node.Annotation)
409+
}
400410

401411
case *Keyword:
402412
// Arg Identifier

ast/walk_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,70 @@ func TestWalk(t *testing.T) {
1717
out []string
1818
}{
1919
{nil, []string{}},
20+
21+
// An empty one of everything
2022
{&Module{}, []string{"*ast.Module"}},
23+
{&Interactive{}, []string{"*ast.Interactive"}},
24+
{&Expression{}, []string{"*ast.Expression"}},
25+
{&Suite{}, []string{"*ast.Suite"}},
26+
{&FunctionDef{}, []string{"*ast.FunctionDef"}},
27+
{&ClassDef{}, []string{"*ast.ClassDef"}},
28+
{&Return{}, []string{"*ast.Return"}},
29+
{&Delete{}, []string{"*ast.Delete"}},
30+
{&Assign{}, []string{"*ast.Assign"}},
31+
{&AugAssign{}, []string{"*ast.AugAssign"}},
32+
{&For{}, []string{"*ast.For"}},
33+
{&While{}, []string{"*ast.While"}},
34+
{&If{}, []string{"*ast.If"}},
35+
{&With{}, []string{"*ast.With"}},
36+
{&Raise{}, []string{"*ast.Raise"}},
37+
{&Try{}, []string{"*ast.Try"}},
38+
{&Assert{}, []string{"*ast.Assert"}},
39+
{&Import{}, []string{"*ast.Import"}},
40+
{&ImportFrom{}, []string{"*ast.ImportFrom"}},
41+
{&Global{}, []string{"*ast.Global"}},
42+
{&Nonlocal{}, []string{"*ast.Nonlocal"}},
43+
{&ExprStmt{}, []string{"*ast.ExprStmt"}},
44+
{&Pass{}, []string{"*ast.Pass"}},
45+
{&Break{}, []string{"*ast.Break"}},
46+
{&Continue{}, []string{"*ast.Continue"}},
47+
{&BoolOp{}, []string{"*ast.BoolOp"}},
48+
{&BinOp{}, []string{"*ast.BinOp"}},
49+
{&UnaryOp{}, []string{"*ast.UnaryOp"}},
50+
{&Lambda{}, []string{"*ast.Lambda"}},
51+
{&IfExp{}, []string{"*ast.IfExp"}},
52+
{&Dict{}, []string{"*ast.Dict"}},
53+
{&Set{}, []string{"*ast.Set"}},
54+
{&ListComp{}, []string{"*ast.ListComp"}},
55+
{&SetComp{}, []string{"*ast.SetComp"}},
56+
{&DictComp{}, []string{"*ast.DictComp"}},
57+
{&GeneratorExp{}, []string{"*ast.GeneratorExp"}},
58+
{&Yield{}, []string{"*ast.Yield"}},
59+
{&YieldFrom{}, []string{"*ast.YieldFrom"}},
60+
{&Compare{}, []string{"*ast.Compare"}},
61+
{&Call{}, []string{"*ast.Call"}},
62+
{&Num{}, []string{"*ast.Num"}},
63+
{&Str{}, []string{"*ast.Str"}},
64+
{&Bytes{}, []string{"*ast.Bytes"}},
65+
{&NameConstant{}, []string{"*ast.NameConstant"}},
66+
{&Ellipsis{}, []string{"*ast.Ellipsis"}},
67+
{&Attribute{}, []string{"*ast.Attribute"}},
68+
{&Subscript{}, []string{"*ast.Subscript"}},
69+
{&Starred{}, []string{"*ast.Starred"}},
70+
{&Name{}, []string{"*ast.Name"}},
71+
{&List{}, []string{"*ast.List"}},
72+
{&Tuple{}, []string{"*ast.Tuple"}},
73+
{&Slice{}, []string{"*ast.Slice"}},
74+
{&ExtSlice{}, []string{"*ast.ExtSlice"}},
75+
{&Index{}, []string{"*ast.Index"}},
76+
{&ExceptHandler{}, []string{"*ast.ExceptHandler"}},
77+
{&Arguments{}, []string{"*ast.Arguments"}},
78+
{&Arg{}, []string{"*ast.Arg"}},
79+
{&Keyword{}, []string{"*ast.Keyword"}},
80+
{&Alias{}, []string{"*ast.Alias"}},
81+
{&WithItem{}, []string{"*ast.WithItem"}},
82+
83+
// Excercise the walk* closures
2184
{&Module{Body: []Stmt{&Pass{}}}, []string{"*ast.Module", "*ast.Pass"}},
2285
{&Module{Body: []Stmt{&Pass{}, &Pass{}}}, []string{"*ast.Module", "*ast.Pass", "*ast.Pass"}},
2386
{&Expression{Body: &Num{}}, []string{"*ast.Expression", "*ast.Num"}},

0 commit comments

Comments
 (0)