forge
Explore
Log in

Don't you dare go Hollow

ec-core: read a display matrix at i32::MIN as Other, not a panic

Browse files at this commit

dcf9b208c7b2191b6c2408e2228d5aaf9f2f0f4aAhmet Kaan GümüşParent: ead0050
Rotation::from_matrix decided its b == -c test with a plain negation, so a
crafted tkhd carrying i32::MIN in the c term panicked in a debug build and
wrapped onto a false quarter turn (Cw270) in a release one. Use checked_neg
so the term falls through to Other, which is the right reading for a value
whose negation is not representable. Add a unit test feeding i32::MIN cases
including the load-bearing (0, MIN, MIN, 0).
crates/ec-core/src/registry.rsmodified+27 -1
@@ -281,7 +281,12 @@ impl Rotation {
281 /// turn this decoder would then apply for real.281 /// turn this decoder would then apply for real.
282 pub fn from_matrix(a: i32, b: i32, c: i32, d: i32) -> Rotation {282 pub fn from_matrix(a: i32, b: i32, c: i32, d: i32) -> Rotation {
283 // A pure rotation: isotropic (a == d, b == -c) with something to turn.283 // A pure rotation: isotropic (a == d, b == -c) with something to turn.
284- if a == d && b == (-c) && (a != 0 || b != 0) {284+ // `c.checked_neg()` rather than `-c`: a crafted `tkhd` can carry
285+ // `i32::MIN`, whose negation is not representable -- the plain form
286+ // panics in a debug build and wraps onto a false quarter turn in a
287+ // release one. `checked_neg` is `None` there, so the term falls through
288+ // to `Other` below, which is the right reading for it anyway.
289+ if a == d && c.checked_neg() == Some(b) && (a != 0 || b != 0) {
285 // `(a, b)` is the turned x-axis; a quarter turn lands it on an axis.290 // `(a, b)` is the turned x-axis; a quarter turn lands it on an axis.
286 return match (a.signum(), b.signum()) {291 return match (a.signum(), b.signum()) {
287 (1, 0) => Rotation::None,292 (1, 0) => Rotation::None,
@@ -617,6 +622,27 @@ mod tests {
617 }622 }
618 }623 }
619 624
625+ #[test]
626+ fn a_matrix_term_at_i32_min_is_not_a_quarter_turn() {
627+ // A crafted `tkhd` can carry `i32::MIN`, whose negation is not
628+ // representable. `from_matrix` must not panic (debug) or wrap onto a
629+ // false turn (release) deciding `b == -c`, and such a term is no
630+ // quarter turn anyway. `(0, MIN, MIN, 0)` is the load-bearing case: the
631+ // wrapping form would read it as `Cw270`.
632+ for (a, b, c, d) in [
633+ (0, i32::MIN, i32::MIN, 0),
634+ (65_536, i32::MIN, i32::MIN, 65_536),
635+ (i32::MIN, 0, i32::MIN, i32::MIN),
636+ ] {
637+ let got = Rotation::from_matrix(a, b, c, d);
638+ assert!(
639+ matches!(got, Rotation::Other { .. }),
640+ "{a} {b} {c} {d} read as {got:?}"
641+ );
642+ assert_eq!(got.steps(), None, "Other is not a quarter count");
643+ }
644+ }
645+
620 #[test]646 #[test]
621 fn trait_objects_are_send() {647 fn trait_objects_are_send() {
622 fn assert_send<T: Send + ?Sized>() {}648 fn assert_send<T: Send + ?Sized>() {}