Fix NULL-stream crash and div-by-zero on malformed WAV channel count - #7
Open
austek wants to merge 1 commit into
Open
Fix NULL-stream crash and div-by-zero on malformed WAV channel count#7austek wants to merge 1 commit into
austek wants to merge 1 commit into
Conversation
1. SEGV in sonicSetSpeed: sonicCreateStream is documented to "Return NULL only if we are out of memory," but main.c's runSonic never checked before immediately calling sonicSetSpeed(stream, ...), dereferencing a NULL stream. Fix: check for NULL after sonicCreateStream, matching the existing fprintf+exit(1) pattern already used for the file-open failure checks just above it. 2. FPE in runSonic (main.c:55): BUFFER_SIZE / numChannels divides by whatever numChannels wave.c's readHeader read straight from the input file's header, with no validation -- unlike the format and bit-depth fields, which are already rejected if invalid. A WAV file with numChannels=0 in its fmt chunk passes readHeader unmodified, then main.c's division by zero raises SIGFPE. Fix: reject numChannels <= 0 in readHeader, the same way the existing format/bit-depth checks already reject other invalid header fields. Adds tests/wave_channel_validation_test.c, wired into this fork's tests/Makefile (which also now links wave.c, needed by the new test). Verified against the real sonic CLI built from this fork's code directly: the 0-channel WAV crashes with SIGFPE before the fix, exits cleanly with an error message after. Ported from waywardgeek/sonic#69, which fixes the same bugs (also present here, byte-identical main.c/wave.c) 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 bugs, same fix as waywardgeek/sonic#69 — this repo's
main.c/wave.care byte-identical to what those files had upstream, so the fix ports directly.SEGV in
sonicSetSpeed:sonicCreateStreamis documented to "Return NULL only if we are out of memory," butmain.c'srunSonicnever checked before immediately callingsonicSetSpeed(stream, ...), dereferencing a NULL stream. Fix: check for NULL aftersonicCreateStream, matching the existingfprintf+exit(1)pattern already used for the file-open failure checks right above it.FPE in
runSonic:BUFFER_SIZE / numChannelsdivides by whatevernumChannelswave.c'sreadHeaderread straight from the input file's header, with no validation — unlike the format and bit-depth fields, which are already rejected if invalid. A WAV file withnumChannels=0in itsfmtchunk passesreadHeaderunmodified, thenmain.c's division raises SIGFPE. Fix: rejectnumChannels <= 0inreadHeader, the same way the existing format/bit-depth checks already reject other invalid header fields.Adds
tests/wave_channel_validation_test.c, wired into this fork'stests/Makefile(which now also linkswave.c, needed by the new test).Test plan
cd tests && make runtests && ./runtests— passes, new test included-fsanitize=address,undefined— no finding from this path; only the separate, already-knownfindSincCoefficientleft-shift issue remainssonicCLI from this fork's code directly and reproduced both fixes end-to-end: the 0-channel WAV crashed with SIGFPE before the fix, exits cleanly with an error message after; normal usage unaffected