Fix overflow in clipping
diff --git a/sparse_strips/vello_common/src/coarse.rs b/sparse_strips/vello_common/src/coarse.rs
index ac8ca43..3245aff 100644
--- a/sparse_strips/vello_common/src/coarse.rs
+++ b/sparse_strips/vello_common/src/coarse.rs
@@ -521,10 +521,10 @@
// Calculate starting position and column for alpha mask
let mut x = x0;
- let mut col = (strip.alpha_idx / u32::from(Tile::HEIGHT)) as u16;
+ let mut col = strip.alpha_idx / u32::from(Tile::HEIGHT);
let clip_x = clip_bbox.x0() * WideTile::WIDTH;
if clip_x > x {
- col += clip_x - x;
+ col += (clip_x - x) as u32;
x = clip_x;
}
@@ -543,10 +543,10 @@
let cmd = CmdClipAlphaFill {
x: x_rel,
width: width as u32,
- alpha_idx: (col * Tile::HEIGHT) as usize,
+ alpha_idx: col as usize * Tile::HEIGHT as usize,
};
x += width;
- col += width;
+ col += width as u32;
// Apply the clip strip command and update state
self.get_mut(wtile_x, cur_wtile_y).clip_strip(cmd);
diff --git a/sparse_strips/vello_cpu/src/render.rs b/sparse_strips/vello_cpu/src/render.rs
index 68931b4..35522e2 100644
--- a/sparse_strips/vello_cpu/src/render.rs
+++ b/sparse_strips/vello_cpu/src/render.rs
@@ -275,8 +275,9 @@
#[cfg(test)]
mod tests {
+ use core::iter;
use crate::RenderContext;
- use vello_common::kurbo::Rect;
+ use vello_common::kurbo::{Rect, Shape};
#[test]
fn reset_render_context() {
@@ -295,4 +296,14 @@
assert!(ctx.strip_buf.is_empty());
assert!(ctx.alphas.is_empty());
}
+
+ #[test]
+ fn clip_overflow() {
+ let mut ctx = RenderContext::new(100, 100);
+
+ ctx.alphas.extend(iter::repeat(255).take(u16::MAX as usize + 1));
+
+ ctx.clip(&Rect::new(20.0, 20.0, 180.0, 180.0).to_path(0.1));
+ ctx.finish();
+ }
}