{"record":{"id":"ba63fff2355a8dac","repo":"tracel-ai/burn","slug":"index-out-of-bounds-for-windowsdataset-index","errorCode":null,"errorMessage":"Index out of bounds for WindowsDataset: {index} >= {}","messagePattern":"Index out of bounds for WindowsDataset: (.+?) >= (.+?)","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-dataset/src/transform/window.rs","lineNumber":171,"sourceCode":"    I: Send + Sync,\n{\n    /// Retrieves a window of items from the dataset.\n    ///\n    /// # Parameters\n    ///\n    /// - `index`: The index of the window.\n    ///\n    /// # Returns\n    ///\n    /// A vector representing the window.\n    ///\n    /// # Panics\n    ///\n    /// Panics if `index >= len()`.\n    fn get(&self, index: usize) -> Result<Vec<I>, DatasetError> {\n        match self.dataset.window(index, self.size)? {\n            Some(window) => Ok(window),\n            None => panic!(\n                \"Index out of bounds for WindowsDataset: {index} >= {}\",\n                self.len()\n            ),\n        }\n    }\n\n    /// Retrieves the number of windows in the dataset.\n    ///\n    /// # Returns\n    ///\n    /// A size representing the number of windows.\n    fn len(&self) -> usize {\n        let len = self.dataset.len() as isize - self.size.get() as isize + 1;\n        max(len, 0) as usize\n    }\n}\n\n#[cfg(test)]","sourceCodeStart":153,"sourceCodeEnd":189,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-dataset/src/transform/window.rs#L153-L189","documentation":"`WindowsDataset::get` asks the wrapped dataset for a sliding window of `self.size` items starting at `index`; when the window provider returns `None` (the window would run past the end of the underlying data), it panics with the index and the number of windows (`self.len()`). The dataset exposes `stride * (n - size) + 1` valid windows; anything beyond that is invalid.","triggerScenarios":"Calling `windows.get(index)` with `index >= windows.len()`; computing the number of windows yourself as `data.len()/stride` instead of using `windows.len()` (which accounts for window size); iterating with the source dataset's length; stride/size changed but loop bounds not updated.","commonSituations":"Time-series datasets where window size + stride interact and the last full window bounds differ from naive division; DataLoader workers using stale lengths; changing `size` or `stride` in config without updating expectations of `len()`.","solutions":["Iterate `0..windows.len()` and let `WindowsDataset` compute valid window count.","If computing manually, use `(data_len - size) / stride + 1` (for size >= 1) rather than `data_len / stride`.","Guard: `if index < windows.len() { windows.get(index) } else { skip }`.","Verify `size` <= underlying dataset length; otherwise `len()` is 0 and any access panics."],"exampleFix":"// before\nlet n = underlying.len() / stride; // ignores window size\nfor i in 0..n { let w = windows.get(i).unwrap(); }\n\n// after\nlet n = windows.len(); // accounts for size and stride\nfor i in 0..n { let w = windows.get(i)?; }","handlingStrategy":"validation","validationCode":"let n_windows = windows.len(); // (src_len - size) / stride + 1\nif index < n_windows {\n    let window = windows.get(index)?;\n} else {\n    // out of range: handle or skip","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Always take the window count from `windows.len()` rather than dividing source length by stride","Verify `size <= source.len()`; otherwise the dataset is empty and any access panics","Update loop bounds when changing window size/stride configuration","Prefer iterating the WindowsDataset directly (it implements Dataset with a correct len) over recomputing bounds"],"tags":["dataset","index-out-of-bounds","windowing","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"}