From dcf9b208c7b2191b6c2408e2228d5aaf9f2f0f4a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ahmet=20Kaan=20G=C3=BCm=C3=BC=C5=9F?= Date: Sat, 19 Sep 2026 23:08:40 +0300 Subject: [PATCH] ec-core: read a display matrix at i32::MIN as Other, not a panic 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.rs | 28 +++++++++++++++++++++++++++- 1 file changed, 27 insertions(+), 1 deletion(-) diff --git a/crates/ec-core/src/registry.rs b/crates/ec-core/src/registry.rs index bae64570..158ed649 100644 --- a/crates/ec-core/src/registry.rs +++ b/crates/ec-core/src/registry.rs @@ -281,7 +281,12 @@ impl Rotation { /// turn this decoder would then apply for real. pub fn from_matrix(a: i32, b: i32, c: i32, d: i32) -> Rotation { // A pure rotation: isotropic (a == d, b == -c) with something to turn. - if a == d && b == (-c) && (a != 0 || b != 0) { + // `c.checked_neg()` rather than `-c`: a crafted `tkhd` can carry + // `i32::MIN`, whose negation is not representable -- the plain form + // panics in a debug build and wraps onto a false quarter turn in a + // release one. `checked_neg` is `None` there, so the term falls through + // to `Other` below, which is the right reading for it anyway. + if a == d && c.checked_neg() == Some(b) && (a != 0 || b != 0) { // `(a, b)` is the turned x-axis; a quarter turn lands it on an axis. return match (a.signum(), b.signum()) { (1, 0) => Rotation::None, @@ -617,6 +622,27 @@ mod tests { } } + #[test] + fn a_matrix_term_at_i32_min_is_not_a_quarter_turn() { + // A crafted `tkhd` can carry `i32::MIN`, whose negation is not + // representable. `from_matrix` must not panic (debug) or wrap onto a + // false turn (release) deciding `b == -c`, and such a term is no + // quarter turn anyway. `(0, MIN, MIN, 0)` is the load-bearing case: the + // wrapping form would read it as `Cw270`. + for (a, b, c, d) in [ + (0, i32::MIN, i32::MIN, 0), + (65_536, i32::MIN, i32::MIN, 65_536), + (i32::MIN, 0, i32::MIN, i32::MIN), + ] { + let got = Rotation::from_matrix(a, b, c, d); + assert!( + matches!(got, Rotation::Other { .. }), + "{a} {b} {c} {d} read as {got:?}" + ); + assert_eq!(got.steps(), None, "Other is not a quarter count"); + } + } + #[test] fn trait_objects_are_send() { fn assert_send() {}