{"record":{"id":"a5641383f0413fd6","repo":"tracel-ai/burn","slug":"index-out-of-bounds-for-wrapped-dataset-size-idx","errorCode":null,"errorMessage":"Index out of bounds for wrapped dataset size: {idx} >= {size}","messagePattern":"Index out of bounds for wrapped dataset size: (.+?) >= (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-dataset/src/transform/selection.rs","lineNumber":83,"sourceCode":"    ///\n    /// # Arguments\n    ///\n    /// * `dataset` - The original dataset to select from.\n    /// * `indices` - A slice of indices to select from the dataset.\n    ///   These indices must be within the bounds of the dataset.\n    ///\n    /// # Panics\n    ///\n    /// Panics if any index is out of bounds for the dataset.\n    pub fn from_indices_checked<S>(dataset: S, indices: Vec<usize>) -> Self\n    where\n        S: Into<Arc<D>>,\n    {\n        let dataset = dataset.into();\n\n        let size = dataset.len();\n        if let Some(idx) = indices.iter().find(|&i| *i >= size) {\n            panic!(\"Index out of bounds for wrapped dataset size: {idx} >= {size}\");\n        }\n\n        Self::from_indices_unchecked(dataset, indices)\n    }\n\n    /// Creates a new selection dataset with the given dataset and indices without checking bounds.\n    ///\n    /// # Arguments\n    ///\n    /// * `dataset` - The original dataset to select from.\n    /// * `indices` - A vector of indices to select from the dataset.\n    ///\n    /// # Safety\n    ///\n    /// This function does not check if the indices are within the bounds of the dataset.\n    pub fn from_indices_unchecked<S>(dataset: S, indices: Vec<usize>) -> Self\n    where\n        S: Into<Arc<D>>,","sourceCodeStart":65,"sourceCodeEnd":101,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-dataset/src/transform/selection.rs#L65-L101","documentation":"`SelectionDataset::from_indices_checked` validates that every index used to select items from the wrapped dataset is within bounds (`idx < dataset.len()`) and panics otherwise, before building the selection dataset via `from_indices_unchecked`. It exists so invalid index lists fail immediately at construction rather than later at `get`.","triggerScenarios":"Calling `SelectionDataset::from_indices_checked(dataset, indices)` where any element of `indices >= dataset.len()`; e.g. building a random selection with an RNG seeded over the wrong range, or selecting from a shrunken/replaced wrapped dataset using old index lists.","commonSituations":"Generating indices with `rand::random::<usize>()` unbounded; sampling k indices from `0..=len` (inclusive off-by-one); reusing indices computed for the full dataset against a filtered/shorter version; cross-split index reuse.","solutions":["Generate indices with `0..dataset.len()` (e.g. `(0..len).choose(&mut rng)` or `rand::seq::index::sample`).","Filter the index list before constructing: `indices.into_iter().filter(|i| *i < len).collect()`.","If indices are intentionally untrusted/out-of-range, use `from_indices_unchecked` only if you also handle skipping at get-time — otherwise fix the source of the indices.","Assert the wrapped dataset is the same one the indices were derived from (same len)."],"exampleFix":"// before\nlet indices: Vec<usize> = (0..1000).map(|_| rng.random_range(0..=len)).collect(); // can equal len -> panic\nlet selection = SelectionDataset::from_indices_checked(dataset, indices);\n\n// after\nlet indices: Vec<usize> = rand::seq::index::sample(&mut rng, len, 1000).into_iter().collect();\nlet selection = SelectionDataset::from_indices_checked(dataset, indices);","handlingStrategy":"validation","validationCode":"let size = dataset.len();\nassert!(indices.iter().all(|&i| i < size), \"all selection indices must be < {size}\");\nlet selection = SelectionDataset::from_indices_checked(dataset, indices);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Sample indices with rand::seq (sample/choose) over 0..len instead of ad-hoc random_range with inclusive bounds","Regenerate index lists whenever the wrapped dataset changes size","Filter indices (`retain(|i| *i < len)`) when constructing selections from untrusted input","Keep index lists paired with the dataset they were derived from (store len alongside)"],"tags":["dataset","index-out-of-bounds","burn","rust"],"backgroundTag":"dataset-index-out-of-bounds","analyzedSha":"d16f7ba2ed0d41408189384044cc886fb4c8f957","analyzedAt":"2026-09-05T13:19:14.260Z","contentChangedAt":"2026-09-05T13:19:14.260Z","schemaVersion":2},"datasetVersion":"2026-09-12T17:17:11.597Z"}