Skip to content

Commit

Permalink
cmd/compile/internal/gc: fix internal compiler error on invalid decla…
Browse files Browse the repository at this point in the history
…ration

Following an empty import, a declaration involving a ? symbol
generates an internal compiler error when the name of the
symbol (in newname function).

package a
import""
var?

go.go:2: import path is empty
go.go:3: internal compiler error: newname nil

Make sure dclname is not called when the symbol is nil.
The error message is now:

go.go:2: import path is empty
go.go:3: invalid declaration
go.go:4: syntax error: unexpected EOF

This CL was initially meant to be applied to the old parser,
and has been updated to apply to the new parser.

Fixes golang#11610

Change-Id: I75e07622fb3af1d104e3a38c89d9e128e3b94522
Reviewed-on: https://go-review.googlesource.com/15268
Reviewed-by: Russ Cox <[email protected]>
  • Loading branch information
dspezia authored and rsc committed Dec 7, 2015
1 parent 715f637 commit 70da2d0
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
16 changes: 14 additions & 2 deletions src/cmd/compile/internal/gc/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -1738,6 +1738,18 @@ func (p *parser) new_name(sym *Sym) *Node {
return nil
}

func (p *parser) dcl_name(sym *Sym) *Node {
if trace && Debug['x'] != 0 {
defer p.trace("dcl_name")()
}

if sym == nil {
yyerrorl(int(prevlineno), "invalid declaration")
return nil
}
return dclname(sym)
}

func (p *parser) onew_name() *Node {
if trace && Debug['x'] != 0 {
defer p.trace("onew_name")()
Expand Down Expand Up @@ -2736,9 +2748,9 @@ func (p *parser) dcl_name_list() *NodeList {
defer p.trace("dcl_name_list")()
}

l := list1(dclname(p.sym()))
l := list1(p.dcl_name(p.sym()))
for p.got(',') {
l = list(l, dclname(p.sym()))
l = list(l, p.dcl_name(p.sym()))
}
return l
}
Expand Down
17 changes: 17 additions & 0 deletions test/fixedbugs/issue11610.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// errorcheck

// Copyright 2015 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// Test an internal compiler error on ? symbol in declaration
// following an empty import.

package a
import"" // ERROR "import path is empty"
var? // ERROR "invalid declaration"

var x int // ERROR "unexpected var"

func main() {
}

0 comments on commit 70da2d0

Please sign in to comment.