Clamp float samples to [-1, 1] before scaling to short - #5
Open
austek wants to merge 1 commit into
Open
Conversation
addFloatSamplesToInputBuffer multiplied each input float by 32767.0f and assigned the result directly into a short, with no range check. sonic.h documents that values must be in [-1, 1], but nothing enforced it, and converting an out-of-range float to short is undefined behavior in C, not just lossy truncation. Every other input path in this file (volume, speed, pitch, rate, sample rate, channel count, and scaleSamples' int16 output) clamps defensively instead of trusting the caller; this path was the exception. Clamp with the existing CLAMP macro before the multiply. Adds sonicTestFloatSampleClamping to tests/input_clamping_test.c, writing out-of-range floats and checking the output stays within [-32767, 32767] via the bit-exact passthrough path that default stream settings take. Ported from waywardgeek/sonic#63, which fixes the same bug (also present here, byte-identical addFloatSamplesToInputBuffer) 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
addFloatSamplesToInputBuffermultiplied each input float by32767.0fand assigned the result directly into ashort, with no range check.sonic.hdocuments that values must be in[-1, 1], but nothing enforced it, and converting an out-of-range float toshortis undefined behavior in C, not just lossy truncation. Every other input path in this file (volume, speed, pitch, rate, sample rate, channel count, andscaleSamples' int16 output) clamps defensively instead of trusting the caller; this path was the exception.Clamps with the existing
CLAMPmacro before the multiply.Same bug, same fix as waywardgeek/sonic#63 — this repo's
addFloatSamplesToInputBufferis byte-identical to what that file had upstream, so the fix ports directly.Test plan
cd tests && make runtests && ./runtests— passes, addssonicTestFloatSampleClampingtotests/input_clamping_test.c-fsanitize=address,undefined— no finding from this path; only the separate, already-knownfindSincCoefficientleft-shift issue remains