{"record":{"id":"6354ba927d65ab44","repo":"tracel-ai/burn","slug":"read-indices-u32-index-v-out-of-isize-range","errorCode":null,"errorMessage":"read_indices: u32 index {v} out of isize range","messagePattern":"read_indices: u32 index (.+?) out of isize range","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-flex/src/ops/gather_scatter.rs","lineNumber":121,"sourceCode":"                })\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| {\n                    isize::try_from(v).unwrap_or_else(|_| {\n                        panic!(\"read_indices: u32 index {v} out of isize range\")\n                    })\n                })\n                .collect(),\n        ),\n        DType::U16 => Cow::Owned(\n            tensor\n                .storage::<u16>()\n                .iter()\n                .map(|&v| v as isize)\n                .collect(),\n        ),\n        DType::U8 => Cow::Owned(tensor.storage::<u8>().iter().map(|&v| v as isize).collect()),\n        other => panic!(\"read_indices: unsupported index dtype {:?}\", other),\n    }\n}\n\n#[cold]\n#[inline(never)]","sourceCodeStart":103,"sourceCodeEnd":139,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-flex/src/ops/gather_scatter.rs#L103-L139","documentation":"read_indices converts U32 index tensors to isize; a u32 value above isize::MAX (possible only on 32-bit targets) cannot be represented, and the library panics instead of producing a wrapped negative index. This protects gather/scatter addressing from silent truncation.","triggerScenarios":"gather, scatter_update, select, select_update, scatter_nd, or gather_nd with a U32 indices tensor whose value exceeds isize::MAX — realistically only on 32-bit platforms (wasm32, armv7) with u32 indices >= 2^31.","commonSituations":"Deploying to wasm32 with large tensors whose flattened indices exceed 2^31, garbage u32 index data, or code that computes indices with u32 arithmetic overflow.","solutions":["Keep tensors and index arithmetic small enough that u32 indices stay below 2^31 on 32-bit targets","Validate indices are < dim_size before the op","Compile for a 64-bit target where u32 always fits isize","Use u8/u16 index dtypes when dimension sizes allow, avoiding the conversion path entirely"],"exampleFix":"// before: u32 flat indices over a huge flattened tensor on wasm32\nlet idx = flat_positions.cast::<u32>();\nlet out = tensor.gather(0, idx);\n// after: chunk so indices stay within 32-bit range, or build for wasm64\nassert!(flat_positions.max_val::<u32>() < isize::MAX as u32);\nlet out = tensor.gather(0, flat_positions.cast::<u32>());","handlingStrategy":"validation","validationCode":"// before calling gather/scatter with a u32 indices tensor (esp. wasm32/armv7)\nlet max_idx = indices.max_val::<u32>();\nassert!((max_idx as u64) < isize::MAX as u64);\nassert!((max_idx as u64) < dim_size as u64);","typeGuard":"fn fits_isize_u32(v: u32) -> bool { (v as u64) < isize::MAX as u64 }","tryCatchPattern":null,"preventionTips":["On 32-bit targets keep flattened tensor sizes below 2^31 elements","Build for 64-bit wasm (wasm64) or native 64-bit when using large index ranges","Clamp u32 indices before the op"],"tags":["panic","integer-overflow","indexing","wasm"],"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"}