Fix integer overflow in insertPitchPeriod/enlargeOutputBufferIfNeeded - #4
Open
austek wants to merge 1 commit into
Open
Fix integer overflow in insertPitchPeriod/enlargeOutputBufferIfNeeded#4austek wants to merge 1 commit into
austek wants to merge 1 commit into
Conversation
period + newSamples is computed correctly in long in insertPitchPeriod, but was truncated to int when passed into enlargeOutputBufferIfNeeded's int parameter. Inside that function, the int addition numOutputSamples + numSamples and the buffer growth calculation can also signed-overflow, both leading to a too-small allocation followed by writes sized for the original, larger sample count. Guard both: insertPitchPeriod now checks the long sum against INT_MAX before truncating, and enlargeOutputBufferIfNeeded checks its own addition for overflow before applying it, returning 0 through the existing failure path in both cases instead of invoking undefined behavior. Adds tests/overflow_test.c, a white-box test that calls both functions directly (they are no longer static, for this reason) with values chosen to hit the overflow guards. Ported from waywardgeek/sonic#62, which fixes the same bug (also present here, byte-identical insertPitchPeriod/enlargeOutputBufferIfNeeded) upstream.
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.
Summary
period + newSamplesis computed correctly inlongininsertPitchPeriod, but was truncated tointwhen passed intoenlargeOutputBufferIfNeeded'sintparameter. Inside that function, theintadditionnumOutputSamples + numSamplesand the buffer growth calculation can also signed-overflow, leading to a too-small allocation followed by writes sized for the original, larger sample count.Guards both:
insertPitchPeriodnow checks thelongsum againstINT_MAXbefore truncating, andenlargeOutputBufferIfNeededchecks its own addition for overflow before applying it, returning 0 through the existing failure path in both cases instead of invoking undefined behavior.Same bug, same fix as waywardgeek/sonic#62 — this repo's
insertPitchPeriod/enlargeOutputBufferIfNeededare byte-identical to what that file had upstream, so the fix ports directly.Test plan
cd tests && make runtests && ./runtests— passes, newtests/overflow_test.cincluded-fsanitize=address,undefined— no finding from this path; only the separate, already-knownfindSincCoefficientleft-shift issue remains