{"record":{"id":"2db30daa8a8deb06","repo":"pola-rs/polars","slug":"range-end-index-end-out-of-range-for-slice-of-length-len","errorCode":null,"errorMessage":"range end index {end} out of range for slice of length {len}","messagePattern":"range end index (.+?) out of range for slice of length (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-utils/src/range.rs","lineNumber":10,"sourceCode":"use std::ops;\n\n// Copied from the stdlib.\nfn slice_index_fail(start: usize, end: usize, len: usize) -> ! {\n    if start > len {\n        panic!(\"range start index {start} out of range for slice of length {len}\",)\n    }\n\n    if end > len {\n        panic!(\"range end index {end} out of range for slice of length {len}\",)\n    }\n\n    if start > end {\n        panic!(\"slice index starts at {start} but ends at {end}\",)\n    }\n\n    // Only reachable if the range was a `RangeInclusive` or a\n    // `RangeToInclusive`, with `end == len`.\n    panic!(\"range end index {end} out of range for slice of length {len}\",)\n}\n\n#[must_use]\npub fn check_range<R>(range: R, bounds: ops::RangeTo<usize>) -> ops::Range<usize>\nwhere\n    R: ops::RangeBounds<usize>,\n{\n    let len = bounds.end;\n    let end = match range.end_bound() {","sourceCodeStart":1,"sourceCodeEnd":28,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-utils/src/range.rs#L1-L28","documentation":"slice_index_fail panics when a range's end exceeds the slice length. This mirrors the standard library's slice bounds message and is reached through polars' check_range helper when a caller requests a slice extending past the end of the underlying data.","triggerScenarios":"Slicing via check_range with an end index greater than the collection length, e.g. asking for rows 5..100 on a 50-row frame segment.","commonSituations":"Hardcoded slice windows computed from stale lengths; pagination that assumes a minimum record count; taking n..end ranges without knowing size.","solutions":["Clamp end to the collection length: end.min(len)","Pre-check end <= len and return an empty/truncated result instead","Use saturating/checked range construction before calling APIs that slice","Fix the upstream length source so the requested end reflects actual data size"],"exampleFix":"// before\nlet out = check_range(start..end, ..len);\n// after\nlet end = end.min(len);\nlet out = check_range(start..end, ..len);","handlingStrategy":"validation","validationCode":"def safe_end(end, len_):\n    if end > len_:\n        raise IndexError(f\"range end {end} out of range for slice of length {len_}\")\n    return end\n# or clamp: end = min(end, len_)","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always clamp end with min(end, len)","Never assume a minimum collection size when paginating","Use checked range helpers consistently","Recompute lengths immediately before slicing"],"tags":["rust","panic","slicing","bounds-check"],"backgroundTag":"index-out-of-range","analyzedSha":"fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e","analyzedAt":"2026-09-18T22:14:11.667Z","contentChangedAt":"2026-09-18T22:14:11.667Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}