{"record":{"id":"8abfe34d4919b6d1","repo":"tracel-ai/burn","slug":"read-indices-i64-index-v-out-of-isize-range","errorCode":null,"errorMessage":"read_indices: i64 index {v} out of isize range","messagePattern":"read_indices: i64 index (.+?) out of isize range","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-flex/src/ops/gather_scatter.rs","lineNumber":68,"sourceCode":"/// ([`gather_f32`], [`select_f32`], ...) already share this helper without a\n/// check, and asymmetry between the int and float paths was what surfaced\n/// the bug.\nfn read_indices(tensor: &FlexTensor) -> Cow<'_, [isize]> {\n    match tensor.dtype() {\n        #[cfg(target_pointer_width = \"64\")]\n        DType::I64 => {\n            const { assert!(size_of::<i64>() == size_of::<isize>()) };\n            let data = tensor.storage::<i64>();\n            Cow::Borrowed(bytemuck::cast_slice(data))\n        }\n        #[cfg(target_pointer_width = \"32\")]\n        DType::I64 => Cow::Owned(\n            tensor\n                .storage::<i64>()\n                .iter()\n                .map(|&v| {\n                    isize::try_from(v).unwrap_or_else(|_| {\n                        panic!(\"read_indices: i64 index {v} out of isize range\")\n                    })\n                })\n                .collect(),\n        ),\n        #[cfg(target_pointer_width = \"64\")]\n        DType::I32 => Cow::Owned(\n            tensor\n                .storage::<i32>()\n                .iter()\n                .map(|&v| v as isize)\n                .collect(),\n        ),\n        #[cfg(target_pointer_width = \"32\")]\n        DType::I32 => {\n            const { assert!(size_of::<i32>() == size_of::<isize>()) };\n            let data = tensor.storage::<i32>();\n            Cow::Borrowed(bytemuck::cast_slice(data))\n        }","sourceCodeStart":50,"sourceCodeEnd":86,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-flex/src/ops/gather_scatter.rs#L50-L86","documentation":"burn-flex's read_indices converts an index tensor to isize values for addressing. When the index tensor's dtype is I64 and a value cannot be represented as isize (only possible on 32-bit targets where isize is i32), the library panics rather than silently truncating or wrapping an index that would address the wrong element. It is a guard against silent integer-truncation bugs in index conversion.","triggerScenarios":"Calling gather, scatter_update, select, select_update, scatter_nd, or gather_nd with an I64 indices tensor containing a value outside the isize range — i.e. an index >= 2^31 (or negative below -2^31) on a 32-bit platform.","commonSituations":"Running burn-flex on 32-bit targets (wasm32, armv7) with indices produced by arithmetic on large values, uninitialized/garbage index data, or an index tensor built from a model exported from a 64-bit environment.","solutions":["Run on a 64-bit target where isize == i64 and every i64 index is representable","Clamp or validate index tensor values to the valid element range before calling gather/scatter ops","Regenerate the indices tensor; garbage values usually indicate a bug upstream (wrong shape, wrong broadcasting)","If indices are legitimately huge, the index tensor is semantically wrong — fix the producer, not the consumer"],"exampleFix":"// before: indices computed by unchecked arithmetic\nlet idx = (positions * stride).int();\nlet out = tensor.gather(0, idx);\n// after: clamp to valid range for the dimension\nlet idx = (positions * stride).clamp(min, dim_size - 1).int();\nlet out = tensor.gather(0, idx);","handlingStrategy":"validation","validationCode":"// before calling gather/scatter with an i64 indices tensor\nlet max_idx = indices.max_val::<i64>();\nlet min_idx = indices.min_val::<i64>();\nassert!(min_idx >= i64::from(isize::MIN) && max_idx <= i64::from(isize::MAX));\nassert!(max_idx < dim_size as i64);","typeGuard":"fn fits_isize_i64(v: i64) -> bool { isize::try_from(v).is_ok() }\n// for the whole tensor, check min/max first","tryCatchPattern":null,"preventionTips":["Prefer 64-bit targets for burn-flex inference","Clamp index tensors to the valid dimension range before gather/scatter","Validate indices produced by upstream arithmetic with min/max assertions"],"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"}