{"record":{"id":"470241bedc77eff3","repo":"flxzt/rnote","slug":"asserting-image-validity-failed-invalid-size-or-d","errorCode":null,"errorMessage":"Asserting image validity failed, invalid size or data.","messagePattern":"Asserting image validity failed, invalid size or data\\.","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/rnote-engine/src/image.rs","lineNumber":190,"sourceCode":"\n    fn rotate(&mut self, angle: f64, center: Vector2) {\n        self.rectangle.rotate(angle, center)\n    }\n\n    fn scale(&mut self, scale: Vector2) {\n        self.rectangle.scale(scale)\n    }\n}\n\nimpl Image {\n    pub fn assert_valid(&self) -> anyhow::Result<()> {\n        self.rectangle.bounds().assert_valid()?;\n\n        if self.pixel_width == 0\n            || self.pixel_height == 0\n            || self.data.len() as u32 != 4 * self.pixel_width * self.pixel_height\n        {\n            Err(anyhow::anyhow!(\n                \"Asserting image validity failed, invalid size or data.\"\n            ))\n        } else {\n            Ok(())\n        }\n    }\n\n    pub fn try_from_encoded_bytes(bytes: &[u8]) -> Result<Self, anyhow::Error> {\n        let reader = ImageReader::new(io::Cursor::new(bytes)).with_guessed_format()?;\n        Ok(Image::from(reader.decode()?))\n    }\n\n    pub fn try_from_cairo_surface(\n        mut surface: cairo::ImageSurface,\n        bounds: Aabb,\n    ) -> anyhow::Result<Self> {\n        let width = surface.width() as u32;\n        let height = surface.height() as u32;","sourceCodeStart":172,"sourceCodeEnd":208,"githubUrl":"https://github.com/flxzt/rnote/blob/bbc5354502ba2fc83eec2670b535348825e679a6/crates/rnote-engine/src/image.rs#L172-L208","documentation":"Image::assert_valid failed because the image has zero width/height or its data buffer length does not equal 4 * pixel_width * pixel_height (RGBA8 bytes per pixel). Called before any consumer (into_imgbuf, into_encoded_bytes, to_memtexture, to_rendernode) touches the data.","triggerScenarios":"Creating an Image with pixel_width or pixel_height == 0, or with a `data` byte buffer whose length mismatches width*height*4; then calling any of the into_*/to_* accessors.","commonSituations":"Off-by-one scaling math when generating surfaces (integer truncation to 0); clipboard/paste handlers storing partial data; serialization bugs where the byte count drifted from the declared dimensions.","solutions":["Fix the producer of the Image so pixel_width/pixel_height match data.len()/4 and are > 0.","Check where dimensions are computed (e.g. width_scaled as u32) and guard against 0 via max(1).","Re-encode/re-download the source image if data arrived truncated.","Call assert_valid() right after constructing Image to fail fast near the real bug."],"exampleFix":"// before\nlet image = Image { pixel_width: w, pixel_height: h, data };\n// after\nassert!(w > 0 && h > 0 && data.len() as u32 == 4 * w * h, \"image size/data mismatch\");\nlet image = Image { pixel_width: w, pixel_height: h, data };","handlingStrategy":"validation","validationCode":"// rust\nfn image_dims_match(img: &Image) -> bool {\n    img.pixel_width > 0 && img.pixel_height > 0\n        && img.data.len() as u32 == 4 * img.pixel_width * img.pixel_height\n}","typeGuard":"fn is_valid_image(img: &Image) -> bool {\n    img.pixel_width.checked_mul(img.pixel_height)\n        .map(|px| px > 0 && img.data.len() as u32 == 4 * px)\n        .unwrap_or(false)\n}","tryCatchPattern":"if let Err(e) = image.assert_valid() {\n    eprintln!(\"discarding invalid image: {e}\");\n    return Ok(None); // or rebuild the image\n}","preventionTips":["Always build Image from dimensions and data produced by the same source call.","Use checked arithmetic for width*height.","Call assert_valid immediately after construction to localize bugs."],"tags":["image","validation","invariant"],"backgroundTag":"invalid-argument-value","analyzedSha":"bbc5354502ba2fc83eec2670b535348825e679a6","analyzedAt":"2026-09-08T13:20:33.747Z","contentChangedAt":"2026-09-08T13:20:33.747Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}