]> Cypherpunks repositories - gostls13.git/commitdiff
cmd/compile: make Type.Field stricter about bounds checking
authorMatthew Dempsky <mdempsky@google.com>
Tue, 15 Mar 2016 18:06:03 +0000 (11:06 -0700)
committerMatthew Dempsky <mdempsky@google.com>
Tue, 15 Mar 2016 18:46:03 +0000 (18:46 +0000)
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>
src/cmd/compile/internal/gc/ssa.go
src/cmd/compile/internal/gc/type.go

index 21a3837d51f8b4421887c2aec7c44430c9c648d6..805420b9668eea363ebb497fc1e4e22aef5c9f03 100644 (file)
@@ -2410,11 +2410,12 @@ func (s *state) call(n *Node, k callKind) *ssa.Value {
 
        // Start exit block, find address of result.
        s.startBlock(bNext)
-       fp := n.Left.Type.Results().Field(0)
-       if fp == nil || k != callNormal {
+       res := n.Left.Type.Results()
+       if res.NumFields() == 0 || k != callNormal {
                // call has no return value. Continue with the next statement.
                return nil
        }
+       fp := res.Field(0)
        return s.entryNewValue1I(ssa.OpOffPtr, Ptrto(fp.Type), fp.Width, s.sp)
 }
 
index fee060e29363643db41eb2cfda6f3ff272f637f3..797174fcabc9c90b1f6e4e4a430f3b33eaedcfb2 100644 (file)
@@ -284,7 +284,14 @@ func (t *Type) Recvs() *Type   { return *t.RecvsP() }
 func (t *Type) Params() *Type  { return *t.ParamsP() }
 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
 // receiver, parameters, and result parameters, in that order.
@@ -309,13 +316,6 @@ func (t *Type) Field(i int) *Field {
                }
                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")
 }