`size_hint` overflow panic for unbounded source

Aperta
#935 0 commenti 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Valutazione

Difficoltà
3/5
Tempo stimato
Mezza giornata
Idoneità per principianti
58/100
Tipo di issue
Bug
Chiarezza
Abbastanza chiara
Stato di attività
Attiva
Stack tecnologico
rust

Direzione di ricerca

Start with ChannelCountConverter::size_hint in src/conversions/channels.rs (around lines 94-99), where the size-hint arithmetic overflows when the source reports (usize::MAX, None). Then check MixerSource::size_hint in the mixer code, which passes through its sources' hints since #831. Reproduce by running the snippet from the issue in a debug build with a Zero source. Done means no overflow panic for unbounded sources and a sensible size hint, such as (0, None), that the maintainers agree on.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Descrizione

During development on the Zed codebase, I ran into a crash related to rodio. Below is what I was able to gather about the problem. Disclaimer: I am not very familiar with Rust and the code base and used AI help to arrive at this diagnosis (this report is still written by me though).

What I observed

ChannelCountConverter::size_hint panics when its source is unbounded (e.g. Zero). The converter does arithmetic on the size hint returned by the underlying source and if that size hint is (usize::MAX, None) we get a panic due to arithmetic overflow at https://github.com/RustAudio/rodio/blob/685e05765c2aff8fbb51e32cda5b18428d09ffbe/src/conversions/channels.rs#L94-L99

Reproducing example

Run the following code in a debug build (release builds don't panic but will silently report a wrong size hint).

let (mixer, source) = rodio::mixer::mixer(nz!(2), nz!(48000));
mixer.add(rodio::source::Zero::new(nz!(1), nz!(48000))); // mono into stereo
let mut source = source;
assert!(source.next().is_some());
let _ = source.size_hint(); // panics: attempt to add with overflow

The core issue seems to affect multiple versions, but in practice the MixerSource was probably able to hide this issue in the past. The MixerSource returned a (0, None) size hint (I checked versions v0.22.2 and v0.23.0), which doesn't cause an overflow. #831 introduced a change causing the mixer to pass-through the size hint of its underlying sources. If the source is Zero (like in the reproduction above), we get a panic. This currently affects master (685e0576).

This behavior can also be observed in 3ee9745 (which is the commit pinned by Zed, which led me to create this issue).

Suggested fix

I'm not familiar enough with the code base to identify the best way to fix this. To me the following seems plausible:

  • Fix the ChannelCountConverter::size_hint implementation to gracefully handle infinite sources
  • Change MixerSource::size_hint to return (0, None) for infinite sources to prevent other consumers from panicking when doing arithmetic on size hints when infinite sources are involved. This is its behavior in v0.22.2 and v0.23.0.

Related

#609 reported the sibling subtract overflow in the same function
#698 additionally suspected delay's size_hint

Lingua principale
Rust
Stelle
2.5k
Fork
338
Merge medio
1g 3h
PR unite (30g)
1

Preparare l'ambiente

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Altre issue di RustAudio/rodio

Tutte le issue di RustAudio/rodio

Issue simili

Altre issue su Rust

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.