ec-core: read a display matrix at i32::MIN as Other, not a panic
Browse files at this commitdcf9b208c7b2191b6c2408e2228d5aaf9f2f0f4aParent: ead00501 changed file +27 -1
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>() {} |