diff --git a/vendor/README.craft-reader.md b/vendor/README.craft-reader.md index 6487d1a59..405799ceb 100644 --- a/vendor/README.craft-reader.md +++ b/vendor/README.craft-reader.md @@ -42,12 +42,17 @@ Consuming workspaces need a `hayro-cmap` patch entry beside the other Hayro entr `hayro-postscript` 0.1.0 is the crates.io package (upstream revision a2354aebaf86f5ab13e5fd6b6b0521306488bf9d), the PostScript token scanner `hayro-cmap` uses -to parse the CMap streams PDFs embed. One local patch: the scanner, which does not parse +to parse the CMap streams PDFs embed. Two local patches. The scanner, which does not parse dictionaries yet, skips runs of `<<` and `>>` in a loop and returns the next object, instead of recursing once per delimiter, so a long run of `<` or `>` bytes no longer overflows the stack. Tests: `object::tests::long_open_dict_run_does_not_overflow`, `object::tests::long_close_dict_run_does_not_overflow`, `object::tests::dict_delimiters_are_skipped` -and `object::tests::nested_dict_delimiters_are_skipped`. Its `SOURCE.json` records the +and `object::tests::nested_dict_delimiters_are_skipped`. A literal string's `\ddd` escape keeps +the low eight bits of its value, as ISO 32000-2 §7.3.4.2 asks ("high-order overflow shall be +ignored"), so `\737` decodes to `0xDF` instead of being dropped; code comments label it +`Craft patch (octal overflow, craft-libs #2321)`. Tests: +`string::tests::literal_octal_overflow_keeps_the_low_eight_bits` and +`string::tests::literal_octal_short_escape_stops_at_a_non_octal_byte`. Its `SOURCE.json` records the registry hashes. Consuming workspaces need a `hayro-postscript` patch entry beside the other Hayro entries. diff --git a/vendor/hayro-postscript/SOURCE.json b/vendor/hayro-postscript/SOURCE.json index 2e4cc5a70..06e922bcc 100644 --- a/vendor/hayro-postscript/SOURCE.json +++ b/vendor/hayro-postscript/SOURCE.json @@ -8,7 +8,8 @@ "upstream_revision": "a2354aebaf86f5ab13e5fd6b6b0521306488bf9d", "upstream_directory": "hayro-postscript", "local_patches": [ - "Skip runs of `<<` and `>>` in a loop in `object::read` instead of recursing once per delimiter, so a long run cannot overflow the stack." + "Skip runs of `<<` and `>>` in a loop in `object::read` instead of recursing once per delimiter, so a long run cannot overflow the stack.", + "Keep the low eight bits of a literal string's `\\ddd` escape in `string::literal::decode_into` (ISO 32000-2 §7.3.4.2), instead of dropping an escape above `\\377`." ], "files": [ { @@ -99,7 +100,7 @@ "path": "src/string.rs", "source_sha256": "1c47947beb5ce7c56036d5b115a47e703a5a276cb9f3faf5e9b2a4304e4a518f", "source_git_blob": null, - "sha256": "1c47947beb5ce7c56036d5b115a47e703a5a276cb9f3faf5e9b2a4304e4a518f" + "sha256": "42bb4c6a7291165f3841333032f582c74145109443f9161065afc6881d2e9f2b" }, { "path": "src/string/ascii_85.rs", @@ -117,7 +118,7 @@ "path": "src/string/literal.rs", "source_sha256": "8633efbb282227512ef506d020ea69d4ef47c549ae9248e7b0d4fcefd2aaf933", "source_git_blob": null, - "sha256": "8633efbb282227512ef506d020ea69d4ef47c549ae9248e7b0d4fcefd2aaf933" + "sha256": "0feae923df7807fe671bc3d320bdcf5e1a65cb7b652aa9245b0b918376c1daf9" } ] } diff --git a/vendor/hayro-postscript/src/string.rs b/vendor/hayro-postscript/src/string.rs index ed39d57c7..26a899847 100644 --- a/vendor/hayro-postscript/src/string.rs +++ b/vendor/hayro-postscript/src/string.rs @@ -187,6 +187,21 @@ mod tests { assert_eq!(decode_literal(b"(\\3)").unwrap(), b"\x03"); } + #[test] + fn literal_octal_overflow_keeps_the_low_eight_bits() { + assert_eq!(decode_literal(b"(\\737)").unwrap(), b"\xDF"); + assert_eq!(decode_literal(b"(\\400\\777)").unwrap(), b"\x00\xFF"); + assert_eq!(decode_literal(b"(\\1234)").unwrap(), b"S4"); + } + + #[test] + fn literal_octal_short_escape_stops_at_a_non_octal_byte() { + assert_eq!( + decode_literal(b"(\\36a\\3z\\0089)").unwrap(), + b"\x1ea\x03z\x0089" + ); + } + #[test] fn literal_line_continuation_lf() { assert_eq!(decode_literal(b"(Hi \\\nthere)").unwrap(), b"Hi there"); diff --git a/vendor/hayro-postscript/src/string/literal.rs b/vendor/hayro-postscript/src/string/literal.rs index 37a15934f..be994f9f3 100644 --- a/vendor/hayro-postscript/src/string/literal.rs +++ b/vendor/hayro-postscript/src/string/literal.rs @@ -12,38 +12,26 @@ pub(crate) fn decode_into(data: &[u8], out: &mut Vec) -> Option<()> { b'\\' => { let next = r.read_byte()?; - if is_octal_digit(next) { - let second = r.read_byte(); - let third = r.read_byte(); - - let bytes = match (second, third) { - (Some(n1), Some(n2)) => match (is_octal_digit(n1), is_octal_digit(n2)) { - (true, true) => [next, n1, n2], - (true, _) => { - r.jump(r.offset() - 1); - [b'0', next, n1] - } - _ => { - r.jump(r.offset() - 2); - [b'0', b'0', next] - } - }, - (Some(n1), None) => { - if is_octal_digit(n1) { - [b'0', next, n1] - } else { - r.jump(r.offset() - 1); - [b'0', b'0', next] - } - } - _ => [b'0', b'0', next], - }; - - let str = core::str::from_utf8(&bytes).unwrap(); - - if let Ok(num) = u8::from_str_radix(str, 8) { - out.push(num); + if let Some(first) = octal_digit(next) { + // Craft patch (octal overflow, craft-libs #2321): read one + // to three octal digits and keep the low eight bits of + // the value. "The number ddd may consist of one, two, or + // three octal digits; high-order overflow shall be + // ignored" (ISO 32000-2 §7.3.4.2; PostScript Language + // Reference, third edition, §3.2.2), so `\737` is 0xDF. + // Previously an escape above `\377` was dropped. + let mut value = first; + for _ in 0..2 { + let Some(digit) = r.peek_byte().and_then(octal_digit) else { + break; + }; + r.forward(); + // At most 0o777 after three digits, so no overflow. + value = value * 8 + digit; } + + // Truncation to the low byte is the overflow rule above. + out.push((value & 0xFF) as u8); } else { match next { b'n' => out.push(0xA), @@ -80,6 +68,9 @@ pub(crate) fn decode_into(data: &[u8], out: &mut Vec) -> Option<()> { Some(()) } -fn is_octal_digit(byte: u8) -> bool { - matches!(byte, b'0'..=b'7') +fn octal_digit(byte: u8) -> Option { + match byte { + b'0'..=b'7' => Some(u16::from(byte - b'0')), + _ => None, + } }