Repository navigation
fix(kernels): refuse a short operand by length in Bin32 and Sqrt32 - #133
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #130
Refs #131 (merged; its benchmark table is quoted below)
What
Bin32,Sqrt32and their scalar oraclesbin32Goandsqrt32Go(internal/kernels/bin32.go) cut their operands witha[:n]. Go checks that reslice againstcap(a), notlen(a). So an operand shorter thandstbut with spare capacity was read past its length, where it should have been refused.Each of the four functions now compares the operand's
lenwithlen(dst)and panics when the operand is shorter. Operands longer thandstare still cut tolen(dst), as before (TestBin32OperandsLongerThanDst).Test
TestShortOperandWithSpareCapacityIsRefusedcovers 18 cases:Bin32andbin32Go, each with the four ops and a shortaor a shortb(16 cases);Sqrt32andsqrt32Gowith a shortsrc(2 cases).In every case
dsthas 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.bin32.go(main), all 18 cases were accepted; the test fails with 18 errors.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 --vetis clean under all 21 CI configurations.🤖 Generated with Claude Code