{"record":{"id":"8044987ce9022ac0","repo":"kitao/pyxel","slug":"canvas-dimensions-are-too-large","errorCode":null,"errorMessage":"canvas dimensions are too large","messagePattern":"canvas dimensions are too large","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/pyxel-core/src/canvas.rs","lineNumber":34,"sourceCode":"pub trait ToIndex {\n    fn to_index(&self) -> usize;\n}\n\n#[derive(Clone)]\npub struct Canvas<T: Copy + PartialEq + Default + ToIndex> {\n    pub self_rect: RectArea,\n    pub clip_rect: RectArea,\n    pub camera_x: i32,\n    pub camera_y: i32,\n    pub alpha: f32,\n    pub data: Vec<T>,\n}\n\nimpl<T: Copy + PartialEq + Default + ToIndex> Canvas<T> {\n    // Constructors\n\n    pub fn new(width: u32, height: u32) -> Self {\n        Self::try_new(width, height).expect(\"canvas dimensions are too large\")\n    }\n\n    pub(crate) fn try_new(width: u32, height: u32) -> Option<Self> {\n        let len = width.checked_mul(height)? as usize;\n        Some(Self {\n            self_rect: RectArea::new(0, 0, width, height),\n            clip_rect: RectArea::new(0, 0, width, height),\n            camera_x: 0,\n            camera_y: 0,\n            alpha: 1.0,\n            data: vec![T::default(); len],\n        })\n    }\n\n    // Public accessors\n\n    pub const fn width(&self) -> u32 {\n        self.self_rect.width()","sourceCodeStart":16,"sourceCodeEnd":52,"githubUrl":"https://github.com/kitao/pyxel/blob/50f9bd77780c993aca62b5b221766bde5791081d/crates/pyxel-core/src/canvas.rs#L16-L52","documentation":"`Canvas::new` allocates a width*height buffer using checked arithmetic; if width*height overflows u32 (or the allocation model), `try_new` returns None and `new` panics with 'canvas dimensions are too large'. It is a guard against impossible/oversized allocations.","triggerScenarios":"Calling Canvas::new(width, height) where width.checked_mul(height) overflows (e.g. very large or zero-derived u32 values), or any caller path where try_new yields None.","commonSituations":"Computing canvas size from user input or resource files without bounds checking; multiplication overflow from malformed data; accidentally passing byte counts or bit-shifted values instead of pixel dimensions.","solutions":["Reduce width/height so their product fits (use Canvas::try_new and handle None to fail gracefully).","Validate dimensions against maximum allowed size before constructing.","Check for logic errors that pass wrong units (bytes vs pixels) or untrusted values."],"exampleFix":"// before\nlet canvas = Canvas::new(w, h);\n// after\nlet canvas = Canvas::try_new(w, h).expect(\"requested canvas size too large\");","handlingStrategy":"validation","validationCode":"fn can_create_canvas(w: u32, h: u32) -> bool {\n    w.checked_mul(h).is_some()\n}","typeGuard":null,"tryCatchPattern":"// panic cannot be caught; use the fallible constructor\nmatch Canvas::try_new(w, h) {\n    Some(c) => c,\n    None => panic!(\"canvas {}x{} too large\", w, h), // handle gracefully instead\n}","preventionTips":["Prefer Canvas::try_new over Canvas::new in code paths with dynamic dimensions.","Clamp width/height to known-good maxima before constructing.","Sanitize dimensions derived from user input or external files."],"tags":["rust","panic","overflow","canvas"],"backgroundTag":"dimensions-too-large","analyzedSha":"50f9bd77780c993aca62b5b221766bde5791081d","analyzedAt":"2026-09-03T10:27:43.285Z","contentChangedAt":"2026-09-03T10:27:43.285Z","schemaVersion":2},"datasetVersion":"2026-09-10T17:17:09.494Z"}