{"record":{"id":"fefbf7382a967661","repo":"flxzt/rnote","slug":"creating-imagesurface-with-dimensions-width-scal","errorCode":null,"errorMessage":"creating ImageSurface with dimensions ({width_scaled}, {height_scaled}) failed, Err: {e:?}","messagePattern":"creating ImageSurface with dimensions \\((.+?), (.+?)\\) failed, Err: (.+?)","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/rnote-engine/src/svg.rs","lineNumber":226,"sourceCode":"        bounds.ensure_positive();\n        bounds.assert_valid()?;\n\n        let svg_data = rnote_compose::utils::wrap_svg_root(\n            self.svg_data.as_str(),\n            Some(bounds),\n            Some(bounds),\n            false,\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 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 ImageSurface with dimensions ({width_scaled}, {height_scaled}) failed, Err: {e:?}\"\n                )\n            })?;\n\n        // Context in new scope, else accessing the surface data fails with a borrow error\n        {\n            let cx =\n                cairo::Context::new(&surface).context(\"creating new cairo::Context failed.\")?;\n            cx.scale(image_scale, image_scale);\n            cx.translate(-bounds.mins[0], -bounds.mins[1]);\n\n            let stream =\n                gio::MemoryInputStream::from_bytes(&glib::Bytes::from(svg_data.as_bytes()));\n\n            let handle = rsvg::Loader::new()\n                .with_unlimited_size(true)\n                .read_stream::<gio::MemoryInputStream, gio::File, gio::Cancellable>(\n                    &stream, None, None,","sourceCodeStart":208,"sourceCodeEnd":244,"githubUrl":"https://github.com/flxzt/rnote/blob/bbc5354502ba2fc83eec2670b535348825e679a6/crates/rnote-engine/src/svg.rs#L208-L244","documentation":"Thrown by Svg::gen_image when cairo::ImageSurface::create(ARgb32, width, height) returns a cairo::Error. Cairo cannot allocate an ARGB32 image surface of the requested pixel dimensions, most often because a dimension is zero, negative, or exceeds cairo's size limits, or memory allocation failed.","triggerScenarios":"width_scaled/height_scaled (bounds extents * image_scale, rounded to i32) are 0, negative, or larger than cairo's maximum surface size (32767 px per dimension), or allocation fails for huge dimensions.","commonSituations":"Rendering an empty selection (zero-sized bounds) to an image, exporting at a very large image_scale producing dimensions beyond cairo limits, or out-of-memory on huge exports.","solutions":["Check width_scaled/height_scaled are > 0 and <= cairo's limits (32767) before calling create; bail with a clear message otherwise","Clamp or cap image_scale so the scaled dimensions stay in range","Reject empty bounds before gen_image (bounds.assert_valid / extents > 0)","Reduce export size or split the drawing into tiles"],"exampleFix":"// before\nlet surface = cairo::ImageSurface::create(cairo::Format::ARgb32, width_scaled as i32, height_scaled as i32).map_err(|e| anyhow::anyhow!(\"creating ImageSurface ... failed, Err: {e:?}\"))?;\n// after\nif width_scaled == 0 || height_scaled == 0 || width_scaled > 32767 || height_scaled > 32767 {\n    anyhow::bail!(\"invalid image dimensions: {width_scaled}x{height_scaled}\");\n}\nlet surface = cairo::ImageSurface::create(cairo::Format::ARgb32, width_scaled as i32, height_scaled as i32)?;","handlingStrategy":"validation","validationCode":"fn valid_image_dims(w: u32, h: u32) -> bool { w > 0 && h > 0 && w <= 32767 && h <= 32767 }","typeGuard":null,"tryCatchPattern":"if !valid_image_dims(width_scaled, height_scaled) {\n    anyhow::bail!(\"refusing to create {width_scaled}x{height_scaled} surface\");\n}\nlet surface = cairo::ImageSurface::create(cairo::Format::ARgb32, width_scaled as i32, height_scaled as i32)?;","preventionTips":["Cap image_scale so scaled dimensions stay under cairo's 32767 px limit","Reject empty/zero-sized bounds before generating images","Watch for integer overflow when casting scaled f64 dimensions to i32"],"tags":["cairo","image","dimensions"],"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"}