Skip to content

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

Native format: refuse a .pcraft with out-of-range numbers when it is opened (#6862)

David Mann921637322cc6
diff --git a/crates/format/src/store.rs b/crates/format/src/store.rs
index d047668cb..7da5dd6b5 100644
--- a/crates/format/src/store.rs
+++ b/crates/format/src/store.rs
@@ -163,6 +163,10 @@ pub(crate) fn read_manifest(src: &dyn ByteSource, opts: &LoadOptions) -> Result<
let layers = take_array(&mut v, "/document/layers");
let mut m: Manifest = serde_json::from_value(v)?;
m.document.layers = layers_from_json(layers)?;
+ // A number past f32's range decodes as infinity, which no save could write back (#6862).
+ if let Err(crate::finite::Problem::NonFinite { path }) = crate::finite::check(&m) {
+ return Err(FormatError::corrupt(format!("`{path}` is out of range")));
+ }
Ok(m)
}
diff --git a/crates/format/tests/corruption.rs b/crates/format/tests/corruption.rs
index d293abed1..727b37cb7 100644
--- a/crates/format/tests/corruption.rs
+++ b/crates/format/tests/corruption.rs
@@ -94,6 +94,24 @@ fn bad_manifest_json() {
assert!(matches!(load_from_bytes(&b), Err(FormatError::Json(_))));
}
+/// A number past f32's range would load as infinity, and every later save would then fail
+/// (#6862): it is refused when the file is opened, naming the value.
+#[test]
+fn out_of_range_number_refused_on_open() {
+ let b = rebuild(&sample(), |n, d| {
+ Some(if n == "manifest.json" {
+ let mut v: serde_json::Value = serde_json::from_slice(&d).unwrap();
+ v["document"]["layers"][0]["opacity"] = serde_json::json!(1e39);
+ serde_json::to_vec(&v).unwrap()
+ } else {
+ d
+ })
+ });
+ let Err(e) = load_from_bytes(&b) else { panic!("a manifest with opacity 1e39 opened") };
+ assert!(matches!(e, FormatError::Corrupt(_)), "{e}");
+ assert!(e.to_string().contains("document.layers[0].opacity"), "{e}");
+}
+
/// Hostile nesting fails with an error instead of overflowing the stack; nesting as deep as the
/// deepest groups a bundle may hold (three JSON levels each) parses, and is then rejected for
/// lacking a format version.