]> Cypherpunks repositories - gostls13.git/commitdiff
go/parser: make sure we have a valid AST when 'if' condition is missing
authorRobert Griesemer <gri@golang.org>
Fri, 18 May 2018 03:56:11 +0000 (23:56 -0400)
committerRobert Griesemer <gri@golang.org>
Fri, 18 May 2018 16:01:32 +0000 (16:01 +0000)
This prevents a crash in go/types due to a nil condition in an 'if'
statement. There's more we can do to make go/types more robust but
this will address the immediate cause and also makes sure that the
parser returns a valid AST in this case.

Fixes #25438.

Change-Id: Ie55dc2c722352a5ecb17af6a16983741e8a8b515
Reviewed-on: https://go-review.googlesource.com/113735
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Daniel Martí <mvdan@mvdan.cc>
Reviewed-by: Dominik Honnef <dominik@honnef.co>
Reviewed-by: Alan Donovan <adonovan@google.com>
src/go/parser/parser.go
src/go/types/testdata/issues.src

index 7671d2a4bb331d7fd009b3bdee1f6b4598e36a79..189bfb42236ecf6b1d44ff5218dd6242036f0231 100644 (file)
@@ -1830,6 +1830,7 @@ func (p *parser) makeExpr(s ast.Stmt, want string) ast.Expr {
 func (p *parser) parseIfHeader() (init ast.Stmt, cond ast.Expr) {
        if p.tok == token.LBRACE {
                p.error(p.pos, "missing condition in if statement")
+               cond = &ast.BadExpr{From: p.pos, To: p.pos}
                return
        }
        // p.tok != token.LBRACE
@@ -1877,6 +1878,11 @@ func (p *parser) parseIfHeader() (init ast.Stmt, cond ast.Expr) {
                }
        }
 
+       // make sure we have a valid AST
+       if cond == nil {
+               cond = &ast.BadExpr{From: p.pos, To: p.pos}
+       }
+
        p.exprLev = outer
        return
 }
index 8c11b376c847228161c7da67c18bc33434971417..da6dc6320a4268240d2b3ba59d593a115be09d5e 100644 (file)
@@ -240,3 +240,11 @@ func issue24140(x interface{}) int {
                 panic(0)
         }
 }
+
+// Test that we don't crash when the 'if' condition is missing.
+func issue25438() {
+       if { /* ERROR missing condition */ }
+       if x := 0; /* ERROR missing condition */ { _ = x }
+       if
+       { /* ERROR missing condition */ }
+}