Skip to content

PhotoCraft: An open-source, photo editor written by scratch in pure Rust

.pcraft: a tile index outside the i32 pixel range is a corrupt file (#938) (#1483)

kayrockscreenprintingb2003a4329bf
diff --git a/crates/format/src/convert.rs b/crates/format/src/convert.rs
index 7fabfa8ab..5cb845a94 100644
--- a/crates/format/src/convert.rs
+++ b/crates/format/src/convert.rs
@@ -310,7 +310,13 @@ impl Loader<'_> {
swap_to_le(&mut dp, f.sample);
let mut s = Surface::with_default(f, &decode_pixel(&f, &dp));
let len = tile_len(&f);
+ // Both edges of a tile's rectangle must fit in i32 (`TileCoord::rect` multiplies by the
+ // tile size): -8388608 ..= 8388606. A damaged manifest can name any index (#938).
+ let fits = |i: i32| i.checked_mul(TILE_SIZE).is_some() && i.checked_add(1).and_then(|e| e.checked_mul(TILE_SIZE)).is_some();
for t in &m.tiles {
+ if !fits(t.tx) || !fits(t.ty) {
+ return Err(FormatError::corrupt(format!("tile ({}, {}) is outside the coordinate range", t.tx, t.ty)));
+ }
let c = TileCoord::new(t.tx, t.ty);
if s.tile(c).is_some() {
return Err(FormatError::corrupt(format!("duplicate tile ({}, {})", t.tx, t.ty)));
diff --git a/crates/format/tests/corruption.rs b/crates/format/tests/corruption.rs
index 303c0ae8a..d3731d971 100644
--- a/crates/format/tests/corruption.rs
+++ b/crates/format/tests/corruption.rs
@@ -329,3 +329,45 @@ fn sample_cached() -> &'static [u8] {
static S: std::sync::OnceLock<Vec<u8>> = std::sync::OnceLock::new();
S.get_or_init(sample)
}
+
+/// Moves the first tile of the first top-level layer that has pixels to `(tx, ty)`; returns that
+/// layer's index.
+fn move_first_tile(v: &mut serde_json::Value, tx: i64, ty: i64) -> Option<usize> {
+ let layers = v["document"]["layers"].as_array_mut()?;
+ let (i, tile) = layers.iter_mut().enumerate().find_map(|(i, l)| l["content"]["surface"]["tiles"].get_mut(0).map(|t| (i, t)))?;
+ tile["tx"] = tx.into();
+ tile["ty"] = ty.into();
+ Some(i)
+}
+
+#[test]
+fn tile_coordinates_outside_the_i32_pixel_range_are_rejected() {
+ // #938: a tile index whose rectangle overflows i32 loaded fine and panicked later, in
+ // `TileCoord::rect`, wherever the layer's bounds were asked for (wrapping in release).
+ let with_tile_at = |tx: i64, ty: i64| {
+ let layer = std::cell::Cell::new(None);
+ let b = rebuild(&sample(), |n, d| {
+ Some(if n == "manifest.json" {
+ let mut v: serde_json::Value = serde_json::from_slice(&d).unwrap();
+ layer.set(move_first_tile(&mut v, tx, ty));
+ serde_json::to_vec(&v).unwrap()
+ } else {
+ d
+ })
+ });
+ (b, layer.get().expect("the sample has a raster layer with a tile"))
+ };
+ let (lo, hi) = (i64::from(i32::MIN / 256), i64::from(i32::MAX / 256 - 1));
+ for (tx, ty) in [(hi + 1, 0), (0, hi + 1), (lo - 1, 0), (0, lo - 1), (i64::from(i32::MAX), 0), (0, i64::from(i32::MIN))] {
+ let err = load_from_bytes(&with_tile_at(tx, ty).0).unwrap_err();
+ assert!(err.to_string().contains("outside the coordinate range"), "({tx}, {ty}): {err}");
+ }
+ // The extreme valid indices still load, and the layer's bounds can be computed.
+ for (tx, ty) in [(hi, hi), (lo, lo), (hi, lo)] {
+ let (b, i) = with_tile_at(tx, ty);
+ let doc = load_from_bytes(&b).unwrap_or_else(|e| panic!("({tx}, {ty}): {e}"));
+ let s = doc.layers[i].surface().expect("raster layer");
+ assert!(s.tiles().any(|(c, _)| (i64::from(c.tx), i64::from(c.ty)) == (tx, ty)), "({tx}, {ty})");
+ assert!(!s.content_bounds().is_empty());
+ }
+}