From c9d3ce5ced282d503102c12de0d6c9494e697515 Mon Sep 17 00:00:00 2001 From: tannevaled Date: Mon, 23 Nov 2026 04:17:42 +0100 Subject: [PATCH] fix(kernels): refuse a short operand by length in Bin32 and Sqrt32 Bin32, Sqrt32 and their scalar oracles cut their operands with a[:n], which Go checks against cap(a), not len(a). An operand shorter than dst but with spare capacity was therefore read past its length instead of refused. Each entry point now compares len(operand) with len(dst) and panics on a shorter one. TestShortOperandWithSpareCapacityIsRefused passes operands of length 3 and capacity 8 (a NaN in the hidden slot) with a dst of length 4 to all 18 entry-point/op/operand combinations; on the previous code all 18 were accepted, now all 18 panic. Fixes #130 Co-Authored-By: Claude Opus 5.5 --- internal/kernels/bin32.go | 14 +++++++++++++ internal/kernels/bin32_test.go | 38 ++++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+) 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