fix: clamp negative Skip n to zero - #21
Merged
Merged
Conversation
Skip(n<0) is documented as "n <= 0 keeps everything" (export.go), but the sizeHint arithmetic max(sizeHint-n, 0) turned a negative n into sizeHint+|n|: SliceOf(1,2,3).Skip(-1).Count() reported 4 while ToSlice() held 3 elements, and ReduceBy's capacity hint was skewed accordingly. Clamp n at entry so all elements are kept and the hint stays exact. Adds TestOps_SkipNonPositiveKeepsAll. Audit finding M2.
tr1v3r
force-pushed
the
fix/skip-negative-hint
branch
from
September 14, 2026 02:31
ea7a68e to
29a3f6d
Compare
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.
Problem
Skip(n)documents"n <= 0 keeps everything"(export.go), and element-wise it does — but the sizeHint arithmeticmax(sizeHint-n, 0)treated a negativenas skipping a negative amount, inflating the hint:Count()short-circuits on the known hint, so it disagreed with the materialized result (audit finding M3 in the audit report / F3 in the core audit).ReduceBy's capacity hint inherits the same skew.Root cause
No input clamp: the "keeps everything" contract was implemented by the iteration loop (nothing skipped) but not by the hint math.
Fix
Clamp at entry —
if n < 0 { n = 0 }— so both the loop andsizeHintarithmetic agree: all elements kept, hint exact,Count() == len(ToSlice()).Test evidence
New
TestOps_SkipNonPositiveKeepsAllcovers:Skip(-5)/Skip(-1)/Skip(0)on a known-size stream:Count()andToSlice()agree (all 3 elements);From(seq, -1).Skip(-1)): same agreement;Skip(1).Skip(-2): hint arithmetic stays exact (Count == 3 of 4).go build/go vetclean,go test ./... -race -count=1all ok,golangci-lint0 issues. Independent of #18/#19/#20 (base: master).