Fix rate-position desync from repeated sonicSetPitch calls - #10
Open
austek wants to merge 1 commit into
Open
Conversation
adjustRate's wraparound arithmetic (oldRatePosition/newRatePosition) assumes both counters were accumulated under one consistent (oldSampleRate, newSampleRate) ratio -- processStreamInput computes that ratio as stream->rate * stream->pitch. sonicSetRate resets both counters to 0 because changing rate invalidates the ratio the counters were accumulated under; sonicSetPitch changes the exact same ratio but never got the same reset. Left to run, the counters drift further from what the (now-changed) ratio expects with every subsequent pitch change. Eventually the drifted counters overflow interpolate()'s position arithmetic, indexing sincTable far out of bounds -- confirmed via ASan: global-buffer-overflow in findSincCoefficient, called from interpolate, called from adjustRate. Fix: reset oldRatePosition/newRatePosition in sonicSetPitch too, mirroring sonicSetRate exactly. Adds tests/pitch_position_test.c with a deterministic repro (10 fixed pitch values, no randomness) -- verified crashing under ASan before this fix, clean after. Ported from waywardgeek/sonic#73, which fixes the same bug (also present here, byte-identical sonicSetPitch/sonicSetRate/adjustRate) 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
Same bug, same fix as waywardgeek/sonic#73 — this repo's
sonicSetPitch/sonicSetRate/adjustRateare byte-identical to what those had upstream, so the fix ports directly.adjustRate's wraparound arithmetic (oldRatePosition/newRatePosition) assumes both counters were accumulated under one consistent(oldSampleRate, newSampleRate)ratio —processStreamInputcomputes that ratio asstream->rate * stream->pitch.sonicSetRateresets both counters to 0 because changing rate invalidates the ratio the counters were accumulated under;sonicSetPitchchanges the exact same ratio but never got the same reset.Left to run, the counters drift further from what the (now-changed) ratio expects with every subsequent pitch change. Eventually the drifted counters overflow
interpolate()'s position arithmetic, indexingsincTablefar out of bounds — confirmed via ASan: global-buffer-overflow infindSincCoefficient, called frominterpolate, called fromadjustRate.Fix
Reset
oldRatePosition/newRatePositioninsonicSetPitchtoo, mirroringsonicSetRateexactly.Adds
tests/pitch_position_test.cwith a deterministic repro (10 fixed pitch values, no randomness) — verified crashing under ASan before this fix, clean after.Test plan
cd tests && make runtests && ./runtests— passes, new test included-fsanitize=address,undefined— no finding from this path; only the separate, already-knownfindSincCoefficientfinding remains