utils/region: Use saturating math for rect extremities
Browse files at this commit2149706ca495b587442921ad72f6a5f0e1f919ffParent: 085f7f81 changed file +27 -3
- src/utils/region.rs +27 -3
src/utils/region.rsmodified+27 -3
| @@ -104,7 +104,7 @@ pub fn region_to_non_overlapping_rects( | |||
| 104 | region | 104 | region |
| 105 | .rects | 105 | .rects |
| 106 | .iter() | 106 | .iter() |
| 107 | - .flat_map(|(_, r)| [r.loc.y, r.loc.y + r.size.h]), | 107 | + .flat_map(|(_, r)| [r.loc.y, r.loc.y.saturating_add(r.size.h)]), |
| 108 | ); | 108 | ); |
| 109 | 109 | ||
| 110 | let mut ys = ys.into_iter(); | 110 | let mut ys = ys.into_iter(); |
| @@ -122,12 +122,12 @@ pub fn region_to_non_overlapping_rects( | |||
| 122 | 122 | ||
| 123 | 'region: for (kind, r) in ®ion.rects { | 123 | 'region: for (kind, r) in ®ion.rects { |
| 124 | // Skip rects that don't overlap with the Y band. | 124 | // Skip rects that don't overlap with the Y band. |
| 125 | - if hi <= r.loc.y || r.loc.y + r.size.h <= lo { | 125 | + if hi <= r.loc.y || r.loc.y.saturating_add(r.size.h) <= lo { |
| 126 | continue; | 126 | continue; |
| 127 | } | 127 | } |
| 128 | 128 | ||
| 129 | let mut x1 = r.loc.x; | 129 | let mut x1 = r.loc.x; |
| 130 | - let mut x2 = r.loc.x + r.size.w; | 130 | + let mut x2 = r.loc.x.saturating_add(r.size.w); |
| 131 | if x1 == x2 { | 131 | if x1 == x2 { |
| 132 | // Empty rect. | 132 | // Empty rect. |
| 133 | continue; | 133 | continue; |
| @@ -279,6 +279,30 @@ mod tests { | |||
| 279 | ); | 279 | ); |
| 280 | } | 280 | } |
| 281 | 281 | ||
| 282 | + // A client can send a region like wl_region.add(0, i32::MAX, 1, 1), and smithay stores the | ||
| 283 | + // coordinates as they came in. Computing the rect extremities must not overflow here, since | ||
| 284 | + // that panics with overflow-checks on. | ||
| 285 | + #[test] | ||
| 286 | + fn test_extremity_overflow() { | ||
| 287 | + let region = RegionAttributes { | ||
| 288 | + rects: vec![ | ||
| 289 | + ( | ||
| 290 | + RectangleKind::Add, | ||
| 291 | + Rectangle::new(Point::new(0, i32::MAX), Size::new(1, 1)), | ||
| 292 | + ), | ||
| 293 | + ( | ||
| 294 | + RectangleKind::Add, | ||
| 295 | + Rectangle::new(Point::new(i32::MAX, 0), Size::new(1, 1)), | ||
| 296 | + ), | ||
| 297 | + ], | ||
| 298 | + }; | ||
| 299 | + | ||
| 300 | + // Both rects are degenerate after clamping, so nothing comes out. | ||
| 301 | + let mut output = Vec::new(); | ||
| 302 | + region_to_non_overlapping_rects(®ion, &mut output); | ||
| 303 | + assert!(output.is_empty()); | ||
| 304 | + } | ||
| 305 | + | ||
| 282 | proptest! { | 306 | proptest! { |
| 283 | #[test] | 307 | #[test] |
| 284 | fn non_overlapping_output( | 308 | fn non_overlapping_output( |