mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 16:24:38 +00:00
fix(ps): bound every decoded string-token append
This commit is contained in:
1 parent
8bd0f08333
commit
6811f1c2b1
1 file changed
+52
-11
+52
-11
@@ -188,6 +188,10 @@ fn string(srcs: &mut Sources, src: usize) -> Result<Vec<u8>, String> {
|
||||
}
|
||||
|
||||
fn hex(srcs: &mut Sources, src: usize) -> Result<Vec<u8>, String> {
|
||||
hex_with_limit(srcs, src, MAX_TOKEN)
|
||||
}
|
||||
|
||||
fn hex_with_limit(srcs: &mut Sources, src: usize, max: usize) -> Result<Vec<u8>, String> {
|
||||
let mut out = Vec::new();
|
||||
let mut hi: Option<u8> = None;
|
||||
loop {
|
||||
@@ -205,20 +209,21 @@ fn hex(srcs: &mut Sources, src: usize) -> Result<Vec<u8>, 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<Vec<u8>, String> {
|
||||
ascii85_with_limit(srcs, src, MAX_TOKEN)
|
||||
}
|
||||
|
||||
fn ascii85_with_limit(srcs: &mut Sources, src: usize, max: usize) -> Result<Vec<u8>, 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<Vec<u8>, 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<Vec<u8>, 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<Vec<u8>, 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<u8>, 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<Obj> {
|
||||
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<Vec<u8>, 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 -.");
|
||||
|
||||
Reference in new issue
Block a user