cmd/compile: fix constant index bounds check and error message

While here, rename nonnegintconst to indexconst (because that's
what it is) and add Fatalf calls where we are not expecting the
indexconst call to fail, and fixed wrong comparison in smallintconst.

Fixes #23781.

Change-Id: I86eb13081c450943b1806dfe3ae368872f76639a
Reviewed-on: https://go-review.googlesource.com/c/151599
Run-TryBot: Robert Griesemer <gri@golang.org>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: Matthew Dempsky <mdempsky@google.com>
This commit is contained in:
Robert Griesemer 2018-11-28 14:34:45 -08:00
parent d029058b59
commit a37d95c74a
5 changed files with 51 additions and 18 deletions

View file

@ -711,7 +711,10 @@ func fixedlit(ctxt initContext, kind initKind, n *Node, var_ *Node, init *Nodes)
var k int64
splitnode = func(r *Node) (*Node, *Node) {
if r.Op == OKEY {
k = nonnegintconst(r.Left)
k = indexconst(r.Left)
if k < 0 {
Fatalf("fixedlit: invalid index %v", r.Left)
}
r = r.Right
}
a := nod(OINDEX, var_, nodintconst(k))
@ -893,7 +896,10 @@ func slicelit(ctxt initContext, n *Node, var_ *Node, init *Nodes) {
var index int64
for _, value := range n.List.Slice() {
if value.Op == OKEY {
index = nonnegintconst(value.Left)
index = indexconst(value.Left)
if index < 0 {
Fatalf("slicelit: invalid index %v", value.Left)
}
value = value.Right
}
a := nod(OINDEX, vauto, nodintconst(index))
@ -1250,7 +1256,10 @@ func initplan(n *Node) {
var k int64
for _, a := range n.List.Slice() {
if a.Op == OKEY {
k = nonnegintconst(a.Left)
k = indexconst(a.Left)
if k < 0 {
Fatalf("initplan arraylit: invalid index %v", a.Left)
}
a = a.Right
}
addvalue(p, k*n.Type.Elem().Width, a)
@ -1260,7 +1269,7 @@ func initplan(n *Node) {
case OSTRUCTLIT:
for _, a := range n.List.Slice() {
if a.Op != OSTRUCTKEY {
Fatalf("initplan fixedlit")
Fatalf("initplan structlit")
}
addvalue(p, a.Xoffset, a.Left)
}