{"record":{"id":"c0dafe431b18bab5","repo":"tracel-ai/burn","slug":"read-indices-u64-index-v-out-of-isize-range","errorCode":null,"errorMessage":"read_indices: u64 index {v} out of isize range","messagePattern":"read_indices: u64 index (.+?) out of isize range","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-flex/src/ops/gather_scatter.rs","lineNumber":101,"sourceCode":"            const { assert!(size_of::<i32>() == size_of::<isize>()) };\n            let data = tensor.storage::<i32>();\n            Cow::Borrowed(bytemuck::cast_slice(data))\n        }\n        DType::I16 => Cow::Owned(\n            tensor\n                .storage::<i16>()\n                .iter()\n                .map(|&v| v as isize)\n                .collect(),\n        ),\n        DType::I8 => Cow::Owned(tensor.storage::<i8>().iter().map(|&v| v as isize).collect()),\n        DType::U64 => Cow::Owned(\n            tensor\n                .storage::<u64>()\n                .iter()\n                .map(|&v| {\n                    isize::try_from(v).unwrap_or_else(|_| {\n                        panic!(\"read_indices: u64 index {v} out of isize range\")\n                    })\n                })\n                .collect(),\n        ),\n        #[cfg(target_pointer_width = \"64\")]\n        DType::U32 => Cow::Owned(\n            tensor\n                .storage::<u32>()\n                .iter()\n                .map(|&v| v as isize)\n                .collect(),\n        ),\n        #[cfg(target_pointer_width = \"32\")]\n        DType::U32 => Cow::Owned(\n            tensor\n                .storage::<u32>()\n                .iter()\n                .map(|&v| {","sourceCodeStart":83,"sourceCodeEnd":119,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-flex/src/ops/gather_scatter.rs#L83-L119","documentation":"Same guard as the I64 case but for U64 index tensors: read_indices converts each u64 to isize via isize::try_from and panics if the value exceeds isize::MAX. u64 values above isize::MAX can never be valid element indices, so the panic prevents silent wraparound to a negative/wrong index.","triggerScenarios":"gather/scatter_update/select/select_update/scatter_nd/gather_nd called with a U64 indices tensor containing a value > isize::MAX (only feasible where isize is 32-bit, or with corrupted/garbage u64 data).","commonSituations":"Corrupted buffers, uninitialized u64 memory interpreted as indices, indices cast from floats with huge values, or 32-bit targets (wasm32) where u64 values above 2^31-1 cannot fit.","solutions":["Validate or clamp the u64 index tensor to [0, dim_size) before the op","Prefer u32/u16/u8 index dtypes on 32-bit targets, where the range naturally fits isize","Audit where the indices are produced; huge u64 values almost always indicate upstream data corruption","Run on a 64-bit target if indices genuinely need the full range"],"exampleFix":"// before: raw u64 indices straight from a decode step\nlet idx: Tensor<..., u64> = decoder.decode();\nlet out = tensor.gather(0, idx);\n// after: clamp into valid range\nlet idx = decoder.decode().clamp(0u64, (dim_size - 1) as u64);\nlet out = tensor.gather(0, idx);","handlingStrategy":"validation","validationCode":"// before calling gather/scatter with a u64 indices tensor\nlet max_idx = indices.max_val::<u64>();\nassert!(max_idx <= isize::MAX as u64);\nassert!(max_idx < dim_size as u64);","typeGuard":"fn fits_isize_u64(v: u64) -> bool { v <= isize::MAX as u64 }","tryCatchPattern":null,"preventionTips":["Use u32 or smaller index dtypes so values always fit isize","Audit producers of u64 index data for corruption","Clamp indices to dim_size bounds before the op"],"tags":["panic","integer-overflow","indexing","rust"],"backgroundTag":"integer-out-of-range-index","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"}