mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-30 18:27:00 +01:00
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:
1 parent
4e0c880f58
commit
d2a7d88d2e
4 files changed
+50
-38
No files matched your search
+24
-33
@@ -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,
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user