forge
Explore
Log in

Don't you dare go Hollow

B2: ec-h264-syntax AnnexBIter::next loops instead of recursing on empty NAL spans

Browse files at this commit

c63953f614afcf7da156ae11988e9d62e6a9a07aAhmet Kaan GümüşParent: 22f0f3f
Repeated 00 00 01 made one recursive call per empty NAL (~350k frames for 1 MB),
overflowing the stack. Regression test added in tests/fuzz_regressions.rs.
Output unchanged for all non-empty NAL units.
crates/ec-h264-syntax/src/nal.rsmodified+18 -20
@@ -148,27 +148,25 @@ impl<'a> Iterator for AnnexBIter<'a> {
148148 type Item = &'a [u8];
149149
150150 fn next(&mut self) -> Option<&'a [u8]> {
151- let start = self.next_start_code(self.pos)? + 3;
152- if start >= self.data.len() {
153- self.pos = self.data.len();
154- return None;
155- }
156- let (end, next_pos) = match self.next_start_code(start) {
157- Some(sc) => (sc, sc),
158- None => (self.data.len(), self.data.len()),
159- };
160- self.pos = next_pos;
161- // Trailing zero bytes before the next start code belong to the start
162- // code prefix / trailing_zero_8bits, not to this NAL unit.
163- let mut end = end;
164- while end > start && self.data[end - 1] == 0 {
165- end -= 1;
166- }
167- if end == start {
168- // Empty NAL unit (all-zero span): skip to the next one.
169- return self.next();
151+ loop {
152+ let start = self.next_start_code(self.pos)? + 3;
153+ if start >= self.data.len() {
154+ self.pos = self.data.len();
155+ return None;
156+ }
157+ let (end, next_pos) = match self.next_start_code(start) {
158+ Some(sc) => (sc, sc),
159+ None => (self.data.len(), self.data.len()),
160+ };
161+ self.pos = next_pos;
162+ let mut end = end;
163+ while end > start && self.data[end - 1] == 0 {
164+ end -= 1;
165+ }
166+ if end > start {
167+ return Some(&self.data[start..end]);
168+ }
170169 }
171- Some(&self.data[start..end])
172170 }
173171 }
174172
crates/ec-h264-syntax/tests/fuzz_regressions.rsmodified+6 -0
@@ -22,3 +22,9 @@ fn pps_scaling_delta_out_of_range_is_corrupt_not_a_panic() {
2222 let err = Pps::parse(&rbsp, |_| None).expect_err("an out-of-range delta_scale is corrupt");
2323 assert!(err.to_string().contains("delta_scale"), "wrong refusal: {err}");
2424 }
25+
26+#[test]
27+fn annexb_repeated_empty_start_codes_do_not_recurse() {
28+ let data = [0u8, 0, 1].repeat(1 << 18);
29+ assert_eq!(AnnexBIter::new(&data).count(), 0);
30+}