{"record":{"id":"cc62cf026c1c4314","repo":"FyroxEngine/Fyrox","slug":"invalid-pixel-position-within","errorCode":null,"errorMessage":"Invalid pixel position: ({}, {}) within ({}, {})","messagePattern":"Invalid pixel position: \\((.+?), (.+?)\\) within \\((.+?), (.+?)\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"fyrox-impl/src/scene/terrain/brushstroke/strokechunks.rs","lineNumber":161,"sourceCode":"    pub fn chunk_to_origin(&self, grid_position: Vector2<i32>) -> Vector2<i32> {\n        Vector2::new(\n            grid_position.x * self.chunk_size.x as i32,\n            grid_position.y * self.chunk_size.y as i32,\n        )\n    }\n    /// The width of the texture in pixels.\n    pub fn row_size(&self) -> usize {\n        match self.kind {\n            TerrainTextureKind::Height => (self.chunk_size.x + 3) as usize,\n            TerrainTextureKind::Mask => self.chunk_size.x as usize,\n        }\n    }\n    /// Calculate the index of a pixel at the given position within texture data,\n    /// based on the row size. The given position is relative to the origin of the texture\n    /// and must be within the bounds of the texture.\n    pub fn pixel_index(&self, position: Vector2<i32>) -> usize {\n        if !self.is_valid_pixel(position) {\n            panic!(\n                \"Invalid pixel position: ({}, {}) within ({}, {})\",\n                position.x, position.y, self.chunk_size.x, self.chunk_size.y\n            );\n        }\n        let p = match self.kind {\n            TerrainTextureKind::Height => position.map(|x| (x + 1) as usize),\n            TerrainTextureKind::Mask => position.map(|x| x as usize),\n        };\n        p.x + p.y * self.row_size()\n    }\n    /// True if the given pixel position is within the bounds of a chunk for the current kind of chunk data.\n    /// Due to the margins of the height textures, it is permitted to index height textures to -1 and chunk_size.x + 1.\n    pub fn is_valid_pixel(&self, position: Vector2<i32>) -> bool {\n        let size = self.chunk_size.map(|x| x as i32);\n        match self.kind {\n            TerrainTextureKind::Height => {\n                (-1..=size.x + 1).contains(&position.x) && (-1..=size.y + 1).contains(&position.y)\n            }","sourceCodeStart":143,"sourceCodeEnd":179,"githubUrl":"https://github.com/FyroxEngine/Fyrox/blob/76c91aad8eca488ce527b1af707be8b3b24ad72d/fyrox-impl/src/scene/terrain/brushstroke/strokechunks.rs#L143-L179","documentation":"StrokeChunks::pixel_index converts a 2D position into a linear index into a chunk's texture data, after validating it with `is_valid_pixel`. It panics when the position lies outside the chunk's `[0, chunk_size)` bounds, preventing an out-of-bounds write into texture memory.","triggerScenarios":"Calling `pixel_index(position)` where `position.x < 0 || position.y < 0 || position.x >= chunk_size.x || position.y >= chunk_size.y` — e.g. painting at a terrain coordinate that maps into a neighboring chunk or beyond the chunk's edge.","commonSituations":"Custom brush code not clamping positions to chunk bounds; brush radius extending past chunk edges; using global terrain coordinates instead of chunk-local coordinates.","solutions":["Call `is_valid_pixel(position)` first and skip/handle invalid positions instead of panicking.","Convert global terrain positions to chunk-local positions before calling pixel_index.","Clamp or split the brush footprint so each position stays within the owning chunk's chunk_size."],"exampleFix":"// before\nlet idx = chunk.pixel_index(position);\n// after\nif chunk.is_valid_pixel(position) {\n    let idx = chunk.pixel_index(position);\n}","handlingStrategy":"validation","validationCode":"if chunk.is_valid_pixel(position) {\n    let idx = chunk.pixel_index(position);\n}","typeGuard":null,"tryCatchPattern":"// Rust panic - avoid instead with is_valid_pixel pre-check (see validationCode).","preventionTips":["Always convert global terrain coordinates to chunk-local coordinates before pixel_index.","Clamp brush footprints to chunk bounds; skip out-of-range pixels.","Add assert-style unit tests around chunk boundary positions (0 and chunk_size - 1)."],"tags":["panic","bounds-check","terrain","texture"],"backgroundTag":"index-out-of-bounds","analyzedSha":"76c91aad8eca488ce527b1af707be8b3b24ad72d","analyzedAt":"2026-09-10T16:04:01.633Z","contentChangedAt":"2026-09-10T16:04:01.633Z","schemaVersion":2},"datasetVersion":"2026-09-16T09:17:16.951Z"}