diff --git a/crates/ps/src/read/scan.rs b/crates/ps/src/read/scan.rs index 9f6aa14c3..166fad5f0 100644 --- a/crates/ps/src/read/scan.rs +++ b/crates/ps/src/read/scan.rs @@ -188,6 +188,10 @@ fn string(srcs: &mut Sources, src: usize) -> Result, String> { } fn hex(srcs: &mut Sources, src: usize) -> Result, String> { + hex_with_limit(srcs, src, MAX_TOKEN) +} + +fn hex_with_limit(srcs: &mut Sources, src: usize, max: usize) -> Result, String> { let mut out = Vec::new(); let mut hi: Option = None; loop { @@ -205,20 +209,21 @@ fn hex(srcs: &mut Sources, src: usize) -> Result, String> { _ => return Err("bad hex string".into()), }; match hi.take() { - Some(h) => out.push(h << 4 | v), + Some(h) => append_string_bytes(&mut out, &[h << 4 | v], max)?, None => hi = Some(v), } - if out.len() > MAX_TOKEN { - return Err("string too long".into()); - } } if let Some(h) = hi { - out.push(h << 4); + append_string_bytes(&mut out, &[h << 4], max)?; } Ok(out) } fn ascii85(srcs: &mut Sources, src: usize) -> Result, String> { + ascii85_with_limit(srcs, src, MAX_TOKEN) +} + +fn ascii85_with_limit(srcs: &mut Sources, src: usize, max: usize) -> Result, String> { let mut out = Vec::new(); let mut group = [0u64; 5]; let mut n = 0; @@ -234,7 +239,7 @@ fn ascii85(srcs: &mut Sources, src: usize) -> Result, String> { break; } if c == b'z' && n == 0 { - out.extend([0; 4]); + append_string_bytes(&mut out, &[0; 4], max)?; continue; } if !(b'!'..=b'u').contains(&c) { @@ -246,12 +251,9 @@ fn ascii85(srcs: &mut Sources, src: usize) -> Result, String> { n += 1; if n == 5 { let v = group.iter().fold(0u64, |a, d| a * 85 + d); - out.extend(u32::try_from(v).map_err(|_| "ASCII85 overflow")?.to_be_bytes()); + append_string_bytes(&mut out, &u32::try_from(v).map_err(|_| "ASCII85 overflow")?.to_be_bytes(), max)?; n = 0; } - if out.len() > MAX_TOKEN { - return Err("string too long".into()); - } } if n > 1 { for g in group.iter_mut().skip(n) { @@ -259,11 +261,21 @@ fn ascii85(srcs: &mut Sources, src: usize) -> Result, String> { } let v = group.iter().fold(0u64, |a, d| a * 85 + d).min(u64::from(u32::MAX)); let v = u32::try_from(v).unwrap_or(u32::MAX); - out.extend(v.to_be_bytes().iter().take(n - 1)); + append_string_bytes(&mut out, v.to_be_bytes().get(..n - 1).unwrap_or_default(), max)?; } Ok(out) } +// Check before every decoded append: zero shorthand and final partial groups can +// produce bytes without passing through the ordinary complete-group branch. +fn append_string_bytes(out: &mut Vec, bytes: &[u8], max: usize) -> Result<(), String> { + if bytes.len() > max.saturating_sub(out.len()) { + return Err("string too long".into()); + } + out.extend_from_slice(bytes); + Ok(()) +} + /// An integer, radix number (`16#FF`) or real; None for anything else. pub fn number(t: &[u8]) -> Option { let s = std::str::from_utf8(t).ok()?; @@ -321,6 +333,35 @@ mod tests { out } + #[test] + fn encoded_strings_obey_the_decoded_budget_at_every_append() { + fn decode(input: &[u8], max: usize, ascii85: bool) -> Result, String> { + let mut sources = Sources::default(); + let id = sources.add(Source::bytes(input.to_vec())); + if ascii85 { ascii85_with_limit(&mut sources, id, max) } else { hex_with_limit(&mut sources, id, max) } + } + for (input, expected) in [ + (b"~>".as_slice(), vec![]), + (b"z~>", vec![0; 4]), + (b"zz~>", vec![0; 8]), + (b"!!!!!~>", vec![0; 4]), + (b"z!!~>", vec![0; 5]), + (b"!!!!!!!!~>", vec![0; 6]), + (b"!!!!~>", vec![0; 3]), + ] { + assert_eq!(decode(input, expected.len(), true).unwrap(), expected); + if !expected.is_empty() { + assert_eq!(decode(input, expected.len() - 1, true).unwrap_err(), "string too long"); + } + } + for (input, expected) in [(b">".as_slice(), vec![]), (b"41>", vec![0x41]), (b"414>", vec![0x41, 0x40]), (b"4>", vec![0x40])] { + assert_eq!(decode(input, expected.len(), false).unwrap(), expected); + if !expected.is_empty() { + assert_eq!(decode(input, expected.len() - 1, false).unwrap_err(), "string too long"); + } + } + } + #[test] fn scans_the_token_kinds() { let t = all(b"12 -3.5 1e3 16#FF /lit name (a\\(b\\)\\101\\\n c) <41 42> <~87cURDZ~> {1 {2} add} //x [ ] << >> % note\n.5 1.e2 -.");