mirror of
https://github.com/golang/go
synced 2024-11-25 09:17:57 -07:00
go/printer: don't crash if AST contains BadXXX nodes
R=golang-dev, rsc CC=golang-dev https://golang.org/cl/5535048
This commit is contained in:
parent
b1bad5530a
commit
a30b172ca0
@ -773,8 +773,13 @@ func (p *printer) print(args ...interface{}) {
|
||||
next = p.fset.Position(x) // accurate position of next item
|
||||
}
|
||||
tok = p.lastTok
|
||||
case string:
|
||||
// incorrect AST - print error message
|
||||
data = x
|
||||
isLit = true
|
||||
tok = token.STRING
|
||||
default:
|
||||
fmt.Fprintf(os.Stderr, "print: unsupported argument type %T\n", f)
|
||||
fmt.Fprintf(os.Stderr, "print: unsupported argument %v (%T)\n", f, f)
|
||||
panic("go/printer type")
|
||||
}
|
||||
p.lastTok = tok
|
||||
|
@ -204,3 +204,18 @@ func init() {
|
||||
panic("got " + s + ", want " + name)
|
||||
}
|
||||
}
|
||||
|
||||
// Verify that the printer doesn't crash if the AST contains BadXXX nodes.
|
||||
func TestBadNodes(t *testing.T) {
|
||||
const src = "package p\n("
|
||||
const res = "package p\nBadDecl\n"
|
||||
f, err := parser.ParseFile(fset, "", src, parser.ParseComments)
|
||||
if err == nil {
|
||||
t.Errorf("expected illegal program")
|
||||
}
|
||||
var buf bytes.Buffer
|
||||
Fprint(&buf, fset, f)
|
||||
if buf.String() != res {
|
||||
t.Errorf("got %q, expected %q", buf.String(), res)
|
||||
}
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user