{"record":{"id":"00b8d9f7d7db0d65","repo":"slint-ui/slint","slug":"insertion-index-is-row-should-be-len-is-len","errorCode":null,"errorMessage":"insertion index (is {row}) should be <= len (is {len})","messagePattern":"insertion index \\(is (.+?)\\) should be <= len \\(is (.+?)\\)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"internal/core/sharedvector.rs","lineNumber":273,"sourceCode":"        self.detach(len);\n        unsafe {\n            let data = data_ptr(self.inner);\n            let value = core::ptr::read(data.add(row));\n            let size = (*self.inner.as_ptr()).header.size;\n            core::ptr::copy(data.add(row + 1), data.add(row), size - 1 - row);\n            (*self.inner.as_ptr()).header.size = size - 1;\n            value\n        }\n    }\n\n    /// Inserts the element at the given index in the array, shifting the following elements.\n    /// If the array was shared, this will make a copy of the array.\n    ///\n    /// Panics if `row > len`.\n    pub fn insert(&mut self, row: usize, value: T) {\n        let len = self.len();\n        if row > len {\n            panic!(\"insertion index (is {row}) should be <= len (is {len})\");\n        }\n        self.detach(capacity_for_grow(self.capacity(), self.len() + 1, core::mem::size_of::<T>()));\n        unsafe {\n            let data = data_ptr(self.inner);\n            let size = (*self.inner.as_ptr()).header.size;\n            core::ptr::copy(data.add(row), data.add(row + 1), size - row);\n            core::ptr::write(data.add(row), value);\n            (*self.inner.as_ptr()).header.size = size + 1;\n        }\n    }\n\n    /// Removes last element from the array and returns it.\n    /// If the array was shared, this will make a copy of the array.\n    pub fn pop(&mut self) -> Option<T> {\n        if self.is_empty() {\n            None\n        } else {\n            self.detach(self.len());","sourceCodeStart":255,"sourceCodeEnd":291,"githubUrl":"https://github.com/slint-ui/slint/blob/bb937076de3f7919766c1f25e2e969367cf77e9a/internal/core/sharedvector.rs#L255-L291","documentation":"SharedVector::insert panics when the insertion index exceeds the current length (row > len). Inserting at exactly len is allowed (append), but anything beyond leaves a gap, so the guard panics before the unsafe element shift. Mirrors Vec::insert's panic semantics.","triggerScenarios":"Calling `vec.insert(row, value)` where row > vec.len() — e.g. inserting at an index computed from a different collection's length, inserting into an empty vector at a nonzero index, or using a stale index after the vector shrank.","commonSituations":"Model insert_row implementations passing UI-supplied positions beyond the data length; arithmetic errors when combining indices; copying indices between similarly-named but differently-sized vectors.","solutions":["Clamp the index before inserting: `let row = row.min(vec.len())`","Check `if row <= vec.len()` before calling insert","Verify the index source (model row count, list selection) matches this vector's current length"],"exampleFix":"// before\nvec.insert(index, value);\n// after\nlet index = index.min(vec.len());\nvec.insert(index, value);","handlingStrategy":"validation","validationCode":"let row = row.min(vec.len());\nvec.insert(row, value);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp insertion indices to the current length","Distinguish append (row == len, allowed) from out-of-range inserts (row > len, panics)","Re-check the vector's length whenever the index comes from another collection"],"tags":["rust","panic","bounds-check","vector"],"backgroundTag":"index-out-of-bounds","analyzedSha":"bb937076de3f7919766c1f25e2e969367cf77e9a","analyzedAt":"2026-09-16T01:37:20.251Z","contentChangedAt":"2026-09-16T01:37:20.251Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}