hayro-postscript: a \ddd escape above \377 keeps its low eight bits (#2321) (#2324)

A literal string's octal escape was converted with `u8::from_str_radix`,
which fails above 0o377, so the byte was dropped. Read one to three octal
digits into a u16 (at most 0o777) and push the low byte, as ISO 32000-2
7.3.4.2 and the PostScript Language Reference (third edition, 3.2.2)
specify: "high-order overflow shall be ignored". Escapes up to `\377`
decode as before.

Record the patch in SOURCE.json and vendor/README.craft-reader.md.

Fixes #2321
This commit is contained in:
mtlcl410 authored and GitHub committed 2026-11-18 23:20:45 -05:00
1 parent 4e0c880f58
commit d2a7d88d2e
4 files changed
+50 -38

No files matched your search

+7 -2
View File
@@ -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.
+4 -3
View File
@@ -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"
}
]
}
+15
View File
@@ -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");
+24 -33
View File
@@ -12,38 +12,26 @@ pub(crate) fn decode_into(data: &[u8], out: &mut Vec<u8>) -> 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<u8>) -> Option<()> {
Some(())
}
fn is_octal_digit(byte: u8) -> bool {
matches!(byte, b'0'..=b'7')
fn octal_digit(byte: u8) -> Option<u16> {
match byte {
b'0'..=b'7' => Some(u16::from(byte - b'0')),
_ => None,
}
}