1
0
mirror of https://github.com/golang/go synced 2024-11-19 09:34:52 -07:00

cmd/compile: make Type.Field stricter about bounds checking

Turns out there were only two call sites that expected
t.Field(t.NumFields()) to return nil.

Change-Id: I4679988d38ee9d7c9d89883537a17046717b2a77
Reviewed-on: https://go-review.googlesource.com/20731
Run-TryBot: Matthew Dempsky <mdempsky@google.com>
TryBot-Result: Gobot Gobot <gobot@golang.org>
Reviewed-by: David Crawshaw <crawshaw@golang.org>
This commit is contained in:
Matthew Dempsky 2016-03-15 11:06:03 -07:00
parent 80a0517129
commit f6fab93a46
2 changed files with 11 additions and 10 deletions

View File

@ -2410,11 +2410,12 @@ func (s *state) call(n *Node, k callKind) *ssa.Value {
// Start exit block, find address of result. // Start exit block, find address of result.
s.startBlock(bNext) s.startBlock(bNext)
fp := n.Left.Type.Results().Field(0) res := n.Left.Type.Results()
if fp == nil || k != callNormal { if res.NumFields() == 0 || k != callNormal {
// call has no return value. Continue with the next statement. // call has no return value. Continue with the next statement.
return nil return nil
} }
fp := res.Field(0)
return s.entryNewValue1I(ssa.OpOffPtr, Ptrto(fp.Type), fp.Width, s.sp) return s.entryNewValue1I(ssa.OpOffPtr, Ptrto(fp.Type), fp.Width, s.sp)
} }

View File

@ -284,7 +284,14 @@ func (t *Type) Recvs() *Type { return *t.RecvsP() }
func (t *Type) Params() *Type { return *t.ParamsP() } func (t *Type) Params() *Type { return *t.ParamsP() }
func (t *Type) Results() *Type { return *t.ResultsP() } func (t *Type) Results() *Type { return *t.ResultsP() }
func (t *Type) Recv() *Field { return t.Recvs().Field(0) } // Recv returns the receiver of function type t, if any.
func (t *Type) Recv() *Field {
s := t.Recvs()
if s.NumFields() == 0 {
return nil
}
return s.Field(0)
}
// recvsParamsResults stores the accessor functions for a function Type's // recvsParamsResults stores the accessor functions for a function Type's
// receiver, parameters, and result parameters, in that order. // receiver, parameters, and result parameters, in that order.
@ -309,13 +316,6 @@ func (t *Type) Field(i int) *Field {
} }
i-- i--
} }
if i == 0 {
// To simplify automated rewrites of existing code, if the
// caller asks for the n'th member of an n-element type,
// return nil instead of panicking.
// TODO(mdempsky): Make callers responsible for bounds checking.
return nil
}
panic("not enough fields") panic("not enough fields")
} }