mirror of
https://github.com/storytold/craft-libs.git
synced 2026-11-18 16:24:38 +00:00
dex: More format parsers keep to their declared lengths and refuse impossible values (#722)
Port Craft Anything b9ca099903aeef5d4b33a7e83e8e7062ab94c65a reader and opcode decoder.
This commit is contained in:
1 parent
e97e73f579
commit
b4e455d017
2 files changed
+17
-14
No files matched your search
+5
-12
@@ -148,18 +148,7 @@ pub fn mutf8(r: &Reader<'_>, mut at: usize, max_units: usize) -> (String, usize)
|
||||
if b0 == 0 {
|
||||
break;
|
||||
}
|
||||
let (u, n) = if b0 < 0x80 {
|
||||
(u16::from(b0), 1)
|
||||
} else if b0 & 0xe0 == 0xc0 {
|
||||
let b1 = r.u8(at + 1).unwrap_or(0);
|
||||
((u16::from(b0 & 0x1f) << 6) | u16::from(b1 & 0x3f), 2)
|
||||
} else if b0 & 0xf0 == 0xe0 {
|
||||
let b1 = r.u8(at + 1).unwrap_or(0);
|
||||
let b2 = r.u8(at + 2).unwrap_or(0);
|
||||
((u16::from(b0 & 0x0f) << 12) | (u16::from(b1 & 0x3f) << 6) | u16::from(b2 & 0x3f), 3)
|
||||
} else {
|
||||
(0xfffd, 1)
|
||||
};
|
||||
let (u, n) = crate::bytes::mutf8_unit(r.data().get(at..).unwrap_or_default());
|
||||
units.push(u);
|
||||
at = at.saturating_add(n);
|
||||
consumed += n;
|
||||
@@ -173,6 +162,10 @@ pub fn sleb128(r: &Reader<'_>, off: usize) -> Option<(i64, usize)> {
|
||||
let mut shift = 0u32;
|
||||
for i in 0..10usize {
|
||||
let b = r.u8(off.checked_add(i)?)?;
|
||||
// Only one value bit remains in the tenth byte; the rest must extend its sign.
|
||||
if i == 9 && !matches!(b, 0x00 | 0x7f) {
|
||||
return None;
|
||||
}
|
||||
result |= i64::from(b & 0x7f).checked_shl(shift)?;
|
||||
shift += 7;
|
||||
if b & 0x80 == 0 {
|
||||
|
||||
@@ -384,6 +384,9 @@ fn payload(code: &[u16], pc: usize) -> Option<(Payload, usize)> {
|
||||
}
|
||||
0x0300 => {
|
||||
let width = *code.get(pc + 1)?;
|
||||
if !matches!(width, 1 | 2 | 4 | 8) {
|
||||
return None;
|
||||
}
|
||||
let count = u32_at(code, pc + 2)?;
|
||||
let bytes = usize::from(width).checked_mul(count as usize)?;
|
||||
let units = bytes.checked_add(1)? / 2 + 4;
|
||||
@@ -513,7 +516,10 @@ pub fn decode_one(code: &[u16], pc: usize) -> Option<Insn> {
|
||||
insn.index = Some(u32::from(u(1)) | (u32::from(u(2)) << 16));
|
||||
}
|
||||
F35c | F45cc => {
|
||||
let count = b4.min(5) as usize;
|
||||
if b4 > 5 {
|
||||
return None;
|
||||
}
|
||||
let count = b4 as usize;
|
||||
let g = a4;
|
||||
let w = u(2);
|
||||
let all = [u32::from(w & 0xf), u32::from((w >> 4) & 0xf), u32::from((w >> 8) & 0xf), u32::from(w >> 12), g];
|
||||
@@ -525,7 +531,11 @@ pub fn decode_one(code: &[u16], pc: usize) -> Option<Insn> {
|
||||
}
|
||||
F3rc | F4rcc => {
|
||||
let first = u32::from(u(2));
|
||||
insn.regs = (0..aa).map(|i| first.saturating_add(i)).collect();
|
||||
let end = first.checked_add(aa)?;
|
||||
if end > u32::from(u16::MAX) + 1 {
|
||||
return None;
|
||||
}
|
||||
insn.regs = (first..end).collect();
|
||||
insn.range = true;
|
||||
insn.index = Some(u32::from(u(1)));
|
||||
if o.fmt == F4rcc {
|
||||
|
||||
Reference in new issue
Block a user