Skip to content

fix(kernels): refuse a short operand by length in Bin32 and Sqrt32 - #133

Merged
tannevaled merged 1 commit into
mainfrom
fix/130-operand-length
Nov 23, 2026
Merged

tannevaled merged 1 commit into
mainfrom
fix/130-operand-length

Conversation

@tannevaled

Copy link
Copy Markdown
Contributor

Fixes #130
Refs #131 (merged; its benchmark table is quoted below)

What

Bin32, Sqrt32 and their scalar oracles bin32Go and sqrt32Go (internal/kernels/bin32.go) cut their operands with a[:n]. Go checks that reslice against cap(a), not len(a). So an operand shorter than dst but with spare capacity was read past its length, where it should have been refused.

Each of the four functions now compares the operand's len with len(dst) and panics when the operand is shorter. Operands longer than dst are still cut to len(dst), as before (TestBin32OperandsLongerThanDst).

Test

TestShortOperandWithSpareCapacityIsRefused covers 18 cases:

  • Bin32 and bin32Go, each with the four ops and a short a or a short b (16 cases);
  • Sqrt32 and sqrt32Go with a short src (2 cases).

In every case dst has length 4 and the short operand has length 3 and capacity 8. Its hidden slot holds a NaN, so an over-read would also show in the result.

  • On the previous bin32.go (main), all 18 cases were accepted; the test fails with 18 errors.
  • With this change, all 18 panic and the test passes.

Performance

Each call adds two length comparisons, or one for Sqrt32, ahead of the kernel. The kernel loop itself is unchanged. This was not benchmarked: the smallest row in #131, Add f32 at n = 1e3, takes about 49 ns, and two integer compares on values already in registers are well below the A/A spread of that row (1.007).

Checks run locally

  • gofmt -l . prints nothing.
  • go test ./... is green.
  • buildscan --vet is clean under all 21 CI configurations.

🤖 Generated with Claude Code

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 <noreply@anthropic.com>
@tannevaled
tannevaled merged commit c1368ce into main Nov 23, 2026
13 checks passed
@tannevaled
tannevaled deleted the fix/130-operand-length branch November 23, 2026 03:33
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

kernels: Bin32 and Sqrt32 read past a short operand's length when it has spare capacity

1 participant