Stop RingBuffer.Resample's index mapping overflowing for large buffers - #76
Merged
Merged
Conversation
…uffers [patch] The source index was computed as i * (oldCount - 1) in int before the double division, so once (length - 1) * (oldCount - 1) passed int.MaxValue the product wrapped negative and oldData[index] threw, after AllocateBuffer had already discarded the buffer's contents. Multiply in double instead. Fixes #73 Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01YXTjHR3MfJpmxt8LEwNkjm
|
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 #73
Resamplecomputed each source index asi * (oldCount - 1)inintand only then divided as adouble. Once(length - 1) * (oldCount - 1)passedint.MaxValue, which happens at about 46k elements, the product wrapped negative andoldData[index]threwIndexOutOfRangeException. By that pointAllocateBufferhad already reset the buffer, so its original contents were lost too.The product is now computed in
double:(double)i * (oldCount - 1) / Math.Max(length - 1, 1). Neither factor is negative, so the index can no longer go below zero, and the existingMath.Minupper clamp is still enough.Tests
Resample_LargeBufferToSameLength_KeepsContents, which resamples 50,000 elements to the same length and expects the same sequence back.Resample_UpsampleToMillionsOfElements_Succeeds, which upsamples 1,000 elements to 3,000,000. The test checks the count and both end values.IndexOutOfRangeException. With the fix they pass.Containers.Testsuite passes (391/391), and the Release build has no warnings.🤖 Generated with Claude Code
https://claude.ai/code/session_01YXTjHR3MfJpmxt8LEwNkjm
Generated by Claude Code