{"record":{"id":"d027eb775ad57da8","repo":"flxzt/rnote","slug":"creating-image-surface-with-dimensions-fa","errorCode":null,"errorMessage":"creating image surface with dimensions ({}, {}) failed, Err: {e:?}","messagePattern":"creating image surface with dimensions \\((.+?), (.+?)\\) failed, Err: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/rnote-engine/src/image.rs","lineNumber":340,"sourceCode":"        image_scale: f64,\n    ) -> anyhow::Result<Self>\n    where\n        F: FnOnce(&cairo::Context) -> anyhow::Result<()>,\n    {\n        bounds.ensure_positive();\n        bounds.loosen(1.0);\n        bounds.assert_valid()?;\n\n        let width_scaled = ((bounds.extents()[0]) * image_scale).round() as u32;\n        let height_scaled = ((bounds.extents()[1]) * image_scale).round() as u32;\n\n        let mut image_surface = cairo::ImageSurface::create(\n            cairo::Format::ARgb32,\n            width_scaled as i32,\n            height_scaled as i32,\n        )\n        .map_err(|e| {\n            anyhow::anyhow!(\n                \"creating image surface with dimensions ({}, {}) failed, Err: {e:?}\",\n                width_scaled,\n                height_scaled,\n            )\n        })?;\n\n        {\n            let cairo_cx = cairo::Context::new(&image_surface)?;\n            cairo_cx.scale(image_scale, image_scale);\n            cairo_cx.translate(-bounds.mins[0], -bounds.mins[1]);\n            // Apply the draw function\n            draw_func(&cairo_cx)?;\n        }\n\n        let data = image_surface\n            .data()\n            .map_err(|e| anyhow::anyhow!(\"accessing image surface data failed, Err: {e:?}\"))?\n            .to_vec();","sourceCodeStart":322,"sourceCodeEnd":358,"githubUrl":"https://github.com/flxzt/rnote/blob/bbc5354502ba2fc83eec2670b535348825e679a6/crates/rnote-engine/src/image.rs#L322-L358","documentation":"cairo::ImageSurface::create failed when allocating an ARgb32 surface of the scaled image dimensions in Image::gen_with_cairo. Usually the requested width/height are zero, negative, or exceed Cairo/platform limits.","triggerScenarios":"Calling gen_with_cairo where width_scaled or height_scaled is <= 0 or absurdly large (integer overflow from scaling math, or cairo's size limit ~32767).","commonSituations":"Zoom values like 0.0 or negative from malformed settings; zoom-out values truncating a 1px image to 0; huge page sizes multiplied by high zoom causing i32 overflow.","solutions":["Clamp width_scaled/height_scaled to at least 1 and below cairo's max surface size before calling create.","Validate the zoom/scale input is finite and > 0 before generating.","Tiled rendering: split very large images into tiles instead of one huge surface.","Check for f64->i32 cast overflow (use saturating casts or checked_mul)."],"exampleFix":"// before\nlet width_scaled = (self.pixel_width as f64 * scale) as i32;\n// after\nlet width_scaled = ((self.pixel_width as f64 * scale) as i32).clamp(1, 32767);","handlingStrategy":"validation","validationCode":"// rust\nfn dims_ok(w: i32, h: i32) -> bool {\n    w > 0 && h > 0 && w <= 32767 && h <= 32767\n}","typeGuard":"fn safe_scale(dim: u32, scale: f64) -> Option<i32> {\n    if !scale.is_finite() || scale <= 0.0 { return None; }\n    Some(((dim as f64 * scale) as i32).clamp(1, 32767))\n}","tryCatchPattern":"match image.gen_with_cairo(scale, draw_fn) {\n    Ok(tex) => tex,\n    Err(e) if e.to_string().contains(\"creating image surface\") => {\n        // retry with clamped dimensions or lower zoom\n    }\n}","preventionTips":["Clamp scaled dimensions to >=1 and below cairo limits.","Reject non-finite or zero zoom values from settings.","Render very large areas in tiles."],"tags":["cairo","image","resource-allocation"],"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-16T09:17:16.951Z"}