Skip to content

Stop RingBuffer.Resample's index mapping overflowing for large buffers - #76

Merged
matt-edmondson merged 1 commit into
mainfrom
claude/containers-73-resample-overflow
Sep 26, 2026
Merged

matt-edmondson merged 1 commit into
mainfrom
claude/containers-73-resample-overflow

Conversation

@matt-edmondson

Copy link
Copy Markdown
Contributor

Fixes #73

Resample computed each source index as i * (oldCount - 1) in int and only then divided as a double. Once (length - 1) * (oldCount - 1) passed int.MaxValue, which happens at about 46k elements, the product wrapped negative and oldData[index] threw IndexOutOfRangeException. By that point AllocateBuffer had 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 existing Math.Min upper clamp is still enough.

Tests

  • Added Resample_LargeBufferToSameLength_KeepsContents, which resamples 50,000 elements to the same length and expects the same sequence back.
  • Added Resample_UpsampleToMillionsOfElements_Succeeds, which upsamples 1,000 elements to 3,000,000. The test checks the count and both end values.
  • I reverted the fix and ran both tests again: each fails with IndexOutOfRangeException. With the fix they pass.
  • The full Containers.Test suite passes (391/391), and the Release build has no warnings.

🤖 Generated with Claude Code

https://claude.ai/code/session_01YXTjHR3MfJpmxt8LEwNkjm


Generated by Claude Code

…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
@sonarqubecloud

Copy link
Copy Markdown

@matt-edmondson
matt-edmondson merged commit b261d39 into main Sep 26, 2026
12 checks passed
@matt-edmondson
matt-edmondson deleted the claude/containers-73-resample-overflow branch September 26, 2026 23:49
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.

RingBuffer.Resample throws IndexOutOfRangeException and loses the buffer's contents for ~46k+ elements (int overflow)

2 participants