WavDecoder::size_hint underflows (u32 wrap, ~4.29e9) after EOF, causes multi-GB Vec::collect() allocation
评估
- 难度
- 3/5
- 预计耗时
- 1-2 天
- 新手友好度
- 72/100
- Issue 类型
- 缺陷
- 描述清晰度
- 描述清楚
- 活跃度
- 活跃
- 技术栈
- rust
调研方向
Start in src/decoder/wav.rs, in SamplesIterator::next and its size_hint, where samples_read is incremented before the read and subtracted from the reader length using u32 arithmetic. Run the repro with a debug build to see the overflow panic, then check how SampleRateConverter reads past the end of its input. Done means size_hint never wraps after EOF, the increment only counts returned samples or stops at len, and the repro's collect() no longer requests gigabytes.
由索引模型根据 Issue 内容生成。
描述
Title: WavDecoder::size_hint underflows (u32 wrap, ~4.29e9) after EOF, causes multi-GB Vec::collect() allocation
Version: rodio 0.22.2 (also present on current master), features: hound
What happens
SamplesIterator::next (src/decoder/wav.rs) increments samples_read unconditionally before reading
the next sample, so it keeps counting when called at/after the end of the data. size_hint then does
self.reader.len() - self.samples_read on u32s. Once samples_read > len this wraps to ~2^32
(release) or panics with "attempt to subtract with overflow" (debug, wav.rs:141).
SampleRateConverter reads past the end of its input, so this is hit by the normal
UniformSourceIterator path. Iterator::collect() uses size_hint().0 to reserve capacity whenever
the Vec has to grow, so depending only on the sample count, collect() requests ~5.7 GB
(2^32 / 3 elements * 4 bytes after 48 kHz -> 16 kHz) and aborts:
memory allocation of 5727255148 bytes failed (exit 134) on systems that refuse the reservation.
In practice this showed up in an application as a crash on ~1 in 3 file lengths.
Repro (Cargo.toml: rodio = { version = "0.22.2", default-features = false, features = ["hound"] })
use rodio::{Decoder, Source, nz};
use std::io::Cursor;
fn wav(num_samples: u32) -> Vec<u8> {
let data_len = num_samples * 2;
let mut b = Vec::new();
b.extend_from_slice(b"RIFF");
b.extend_from_slice(&(36 + data_len).to_le_bytes());
b.extend_from_slice(b"WAVEfmt ");
b.extend_from_slice(&16u32.to_le_bytes());
b.extend_from_slice(&1u16.to_le_bytes()); // PCM
b.extend_from_slice(&1u16.to_le_bytes()); // mono
b.extend_from_slice(&48000u32.to_le_bytes());
b.extend_from_slice(&96000u32.to_le_bytes());
b.extend_from_slice(&2u16.to_le_bytes());
b.extend_from_slice(&16u16.to_le_bytes());
b.extend_from_slice(b"data");
b.extend_from_slice(&data_len.to_le_bytes());
b.resize(b.len() + data_len as usize, 0);
b
}
fn main() {
// Minimal: one extra next() after the end
let mut decoder = Decoder::new(Cursor::new(wav(10))).unwrap();
while decoder.next().is_some() {}
decoder.next();
println!("{:?}", decoder.size_hint()); // release: (4294967294, Some(4294967294)); debug: panic
// Via the resampler: collect() asks for ~5.7 GB when len happens to trigger a reallocation
let decoder = Decoder::new(Cursor::new(wav(474_067))).unwrap();
let v: Vec<f32> = rodio::source::UniformSourceIterator::new(decoder, nz!(1), nz!(16000)).collect();
println!("{}", v.len());
}
Suggested fix: in next(), only increment samples_read when a sample was actually returned
(or at least don't count past len), and use saturating_sub in size_hint.
Workaround for callers: don't use collect()/extend() on rodio sources; push into a Vec in a loop.
- 主要语言
- Rust
- 星标
- 2.5k
- 派生
- 338
- 平均合并
- 1 天 3 小时
- 30 天内合并 PR
- 1
环境准备
- 没有 Dockerfile 或 Docker Compose 文件
- 没有 Pull Request 模板
- 阅读贡献指南
从这里开始
- 先读完整个 Issue,再读项目的贡献指南。
- 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
- Fork 仓库,在一个分支上完成修改。
- 提交 Pull Request,并在描述里引用这个 Issue 编号。
RustAudio/rodio 的其他 Issue
-
bug
难度 2/5 1-3 小时 新手友好度 74/100
-
难度 1/5 1 小时以内 新手友好度 73/100
-
难度 3/5 1-2 天 新手友好度 62/100
-
难度 4/5 3-5 天 新手友好度 45/100
-
难度 4/5 3-5 天 新手友好度 35/100
相似的 Issue
-
难度 2/5 1-3 小时 新手友好度 68/100
plrigaux/sysd-manager#85 ·
-
难度 2/5 1-3 小时 新手友好度 72/100
dsx-ai-factory/infra-controller#8341 ·
维护者通常 1 天内回复
-
enhancement triage:deciding
难度 2/5 1-3 小时 新手友好度 70/100
open-telemetry/otel-arrow#4540 ·
维护者通常 2 天内回复
-
benchmark: CSV report files contain blank records on Windows可能已有人在做 关联的 PR 仍在进行中或已合并。 未关闭component:benchmark
难度 2/5 1-3 小时 新手友好度 76/100
agentic-os-org/ANOLISA#9177 ·
维护者通常 1 天内回复
-
area/mcp area/modeling effort/s impact/few priority/p3 severity/low triaged type/bug urgency/this-quarter
难度 2/5 1-3 小时 新手友好度 74/100
cristim/AztecCraft#336 ·
维护者通常 1 天内回复