diff --git a/internal/kernels/bin32.go b/internal/kernels/bin32.go index cd37cd9..f13564c 100644 --- a/internal/kernels/bin32.go +++ b/internal/kernels/bin32.go @@ -17,6 +17,11 @@ const ( // operation, so the result is the scalar loop's bit for bit. func Bin32(op int, dst, a, b []float32) { n := len(dst) + // By length, not by reslice: a[:n] is checked against cap(a), so a short + // operand with spare capacity would be over-read (#130). + if len(a) < n || len(b) < n { + panic("kernels: Bin32 operand shorter than dst") + } if n == 0 { return } @@ -26,6 +31,9 @@ func Bin32(op int, dst, a, b []float32) { // bin32Go is the scalar oracle the kernels are tested against, and the // implementation where there is no kernel. func bin32Go(op int, dst, a, b []float32) { + if len(a) < len(dst) || len(b) < len(dst) { + panic("kernels: Bin32 operand shorter than dst") + } a, b = a[:len(dst)], b[:len(dst)] switch op { case Add32: @@ -56,6 +64,9 @@ func bin32Go(op int, dst, a, b []float32) { // twice float32's precision. func Sqrt32(dst, src []float32) { n := len(dst) + if len(src) < n { // by length: src[:n] is checked against cap (#130) + panic("kernels: Sqrt32 src shorter than dst") + } if n == 0 { return } @@ -65,6 +76,9 @@ func Sqrt32(dst, src []float32) { // sqrt32Go is the scalar oracle for Sqrt32, and the implementation where // there is no kernel. func sqrt32Go(dst, src []float32) { + if len(src) < len(dst) { + panic("kernels: Sqrt32 src shorter than dst") + } src = src[:len(dst)] for i := range dst { dst[i] = float32(math.Sqrt(float64(src[i]))) diff --git a/internal/kernels/bin32_test.go b/internal/kernels/bin32_test.go index 12d35ea..2b29d66 100644 --- a/internal/kernels/bin32_test.go +++ b/internal/kernels/bin32_test.go @@ -1,6 +1,7 @@ package kernels import ( + "fmt" "math" "testing" ) @@ -115,6 +116,43 @@ func TestBin32RefusesBadInput(t *testing.T) { } } +// TestShortOperandWithSpareCapacityIsRefused is #130: a reslice x[:n] is +// checked against cap(x), not len(x), so an operand of length 3 and capacity 8 +// passed with a dst of length 4 used to be read one element past its length. +// Every entry point must refuse it by length. The hidden element is a NaN so +// an over-read that slipped through would also show in the result. +func TestShortOperandWithSpareCapacityIsRefused(t *testing.T) { + short := func() []float32 { + s := make([]float32, 8) + for i := range s { + s[i] = float32(i + 1) + } + s[3] = float32(math.NaN()) + return s[:3] // len 3, cap 8 + } + full := func() []float32 { return []float32{1, 2, 3, 4} } + cases := map[string]func(){} + for op := Add32; op <= Div32; op++ { + op := op + cases[fmt.Sprintf("Bin32 op %d short a", op)] = func() { Bin32(op, make([]float32, 4), short(), full()) } + cases[fmt.Sprintf("Bin32 op %d short b", op)] = func() { Bin32(op, make([]float32, 4), full(), short()) } + cases[fmt.Sprintf("bin32Go op %d short a", op)] = func() { bin32Go(op, make([]float32, 4), short(), full()) } + cases[fmt.Sprintf("bin32Go op %d short b", op)] = func() { bin32Go(op, make([]float32, 4), full(), short()) } + } + cases["Sqrt32 short src"] = func() { Sqrt32(make([]float32, 4), short()) } + cases["sqrt32Go short src"] = func() { sqrt32Go(make([]float32, 4), short()) } + for name, f := range cases { + func() { + defer func() { + if recover() == nil { + t.Errorf("%s: a short operand with spare capacity was accepted", name) + } + }() + f() + }() + } +} + // TestBin32OperandsLongerThanDst pins the contract the floats kernels need: they // process len(a) elements and panic on a shorter operand, so Bin32 and Sqrt32 // must hand them operands of exactly len(dst) -- longer inputs are cut, shorter