audio: validate arguments before narrowing them, and end an MP3 with … - #11368
Open
peterbay wants to merge 1 commit into
Open
audio: validate arguments before narrowing them, and end an MP3 with …#11368peterbay wants to merge 1 commit into
peterbay wants to merge 1 commit into
Conversation
…DONE audiomixer narrowed the voice index into a uint8_t before comparing it with voice_count, so voice=256 became 0 and played on the first voice. PDMIn did the same with bit_depth and oversample, ahead of the divisibility check that 256 then passed as zero, and never checked sample_rate. The sample_rate setter shared by every audiosample validated nothing at all, so it could undo the constructors' own check. I2SOut's left_justified was declared MP_ARG_OBJ and read as .u_bool, so passing it explicitly gave whatever the object pointer aliased to. audiomixer's mono path stepped two samples at a time and wrote word_buffer[i] and word_buffer[i + 1], one past the end for an odd n. An input underflow at the end of the data is how an MP3 cut off mid-frame ends, not a failure, but get_buffer returned GET_BUFFER_ERROR for it and a looped playback stopped instead of restarting. It returns GET_BUFFER_DONE for an underflow at end of file now. stream_lseek was also given its offset and whence the wrong way round in two places, and the success test compared against zero where a stream that cannot seek returns its unchanged position.
dhalbert
approved these changes
Sep 13, 2026
dhalbert
left a comment
Collaborator
There was a problem hiding this comment.
Thanks - these are nice fixes, and they all make sense to me.
Author
|
Testing and diagnostic script. |
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.
Code written by Claude Code, guided and corrected by @peterbay.
The problem
Five defects across the audio modules. Four are arguments narrowed into a
uint8_toruint32_tfield before anything checked them, so a value past the field's range wrapped into a valid-looking one. The fifth stops a looped MP3 from restarting.The changes
audiomp3returned an error where a stream had simply ended. An input underflow at the end of the data is how an MP3 that was cut off mid-frame finishes, not a failure, butget_bufferreturnedGET_BUFFER_ERRORfor it and the caller stopped playback instead of looping. It returnsGET_BUFFER_DONEwhen the underflow is at end of file, andGET_BUFFER_ERRORfor a real decode error as before.audiomp3passedstream_lseekits offset and whence the wrong way round, in two places. The signature is(stream, offset, whence). The seek past an ID3 header asked to moveSEEK_CURbytes from whencesize, failed, and fell through to the read-and-discard loop below, which is why it still worked. The rewind on loop passed(SEEK_SET, 0), which worked only becauseSEEK_SETis 0. The success test on the first one is also wrong: a stream that cannot seek returns its unchanged position, so comparing against zero is not enough. It now compares the position before and after.audiomixernarrowed the voice index before checking it.args[ARG_voice].u_intwent into auint8_tand was then compared againstvoice_count - 1, sovoice=256became 0 and played on the first voice instead of raising. Changed tomp_arg_validate_int_rangeon the value as parsed.audiomixerwrote one word past the mix buffer. The mono path steppedfor (i = 0; i < n; i += 2)and wroteword_buffer[i]andword_buffer[i + 1], so an oddnwrote one past the end. The loop now boundsi + 1.audiobusio.PDMInnarrowedbit_depthandoversamplebefore the divisibility check, so 256 passed it as zero, andsample_ratewas not checked at all. All three are validated on the parsed value now.audiocore's sharedsample_ratesetter validated nothing. It is mixed into every audio sample throughAUDIOSAMPLE_FIELDS, so it could undo the constructors' own check; zero reached code that divides by it.I2SOut.left_justifiedwas declaredMP_ARG_OBJand read as.u_bool. Passing it explicitly stored an object in the union and the flag came back as whatever that pointer aliased to, soleft_justified=Falseread as true. DeclaredMP_ARG_BOOL.Testing
Seeed XIAO nRF52840 Sense with an Adafruit Audio BFF, on two builds differing only by these changes. Playback ran through a mixer voice held at level 0, so the decoder was driven without anything audible.
mixer.play(sample, voice=256)on a 2-voice mixerValueError: voice must be 0-1sample.sample_rate = 0ValueError: sample_rate must be >= 1PDMIn(..., oversample=256)ValueError: oversample must be multiple of 8.loop=TruePDMIn'sbit_depth=256andsample_rate=0are rejected on this port either way, because its HAL supports onlybit_depth=16andsample_rate=16000and refuses them for its own reason;oversampleis the one it leaves to the binding. A complete MP3 never reaches the changed branch — it ends on a whole frame — so the loop test used a truncated copy.Two of these are not covered above. The mixer's one-word overrun needs an odd
n, which the buffer sizes here do not produce.left_justifiedsetsNRF_I2S->CONFIG.FORMATon this port, butmemorymaphere only maps RAM, so the register could not be read back from Python.