{"record":{"id":"f9861893f2afc742","repo":"pola-rs/polars","slug":"range-start-index-start-out-of-range-for-slice-of-length-len","errorCode":null,"errorMessage":"range start index {start} out of range for slice of length {len}","messagePattern":"range start index (.+?) out of range for slice of length (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/polars-utils/src/range.rs","lineNumber":6,"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","sourceCodeStart":1,"sourceCodeEnd":24,"githubUrl":"https://github.com/pola-rs/polars/blob/fe841f959ef4d2ceefc05a310d33ed7b1ab24e5e/crates/polars-utils/src/range.rs#L1-L24","documentation":"slice_index_fail in polars-utils' range helper replicates stdlib slice-index panics: it aborts when a range's start exceeds the slice length. check_range funnels invalid RangeBounds here, so any API using check_range with start > len triggers this message.","triggerScenarios":"Calling an API that indexes/slices via check_range with a RangeFrom or Range whose start exceeds the collection length (e.g. slicing a row/column collection at an offset past the end).","commonSituations":"Computing offsets from mismatched lengths (empty input data); off-by-one when slicing after a filter; passing user-supplied offsets without clamping.","solutions":["Clamp start to the collection length before slicing: start.min(len)","Check len == 0 or start >= len and return an empty result instead of slicing","Validate user-supplied start indices against the actual size first","Trace the caller of check_range to fix the length computation upstream"],"exampleFix":"// before\nlet out = check_range(start..end, ..len);\n// after\nlet start = start.min(len);\nlet out = check_range(start..end, ..len);","handlingStrategy":"validation","validationCode":"def safe_slice(start, end, len_):\n    start = max(0, min(start, len_))\n    end = max(start, min(end, len_))\n    return start, end","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp offsets against actual lengths before slicing","Handle empty collections (len 0) explicitly","Avoid deriving offsets from stale/prior data versions","Wrap user-supplied indices in a validated accessor"],"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"}