{"record":{"id":"a12a0221e4561cd9","repo":"tracel-ai/burn","slug":"hannwindow-size-doesn-t-fit-in-i64-range","errorCode":null,"errorMessage":"HannWindow size doesn't fit in i64 range.","messagePattern":"HannWindow size doesn't fit in i64 range\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-tensor/src/tensor/signal/hann_window.rs","lineNumber":51,"sourceCode":"pub fn hann_window(\n    size: usize,\n    periodic: bool,\n    options: impl Into<TensorCreationOptions>,\n) -> Tensor<1> {\n    let opt = options.into();\n    let dtype = opt.resolve_dtype::<Float>();\n    let shape = [size];\n    check!(TensorCheck::creation_ops::<1>(\"HannWindow\", &shape));\n\n    if size == 0 {\n        return Tensor::<1>::empty(shape, opt).cast(dtype);\n    }\n\n    if size == 1 {\n        return Tensor::<1>::ones(shape, opt).cast(dtype);\n    }\n\n    let size_i64 = i64::try_from(size).expect(\"HannWindow size doesn't fit in i64 range.\");\n    let denominator = if periodic { size } else { size - 1 };\n    let angular_increment = (2.0 * core::f64::consts::PI) / denominator as f64;\n\n    Tensor::<1, Int>::arange(0..size_i64, &opt.device)\n        .float()\n        .mul_scalar(angular_increment)\n        .cos()\n        .mul_scalar(-0.5)\n        .add_scalar(0.5)\n        .cast(dtype)\n}\n","sourceCodeStart":33,"sourceCodeEnd":63,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-tensor/src/tensor/signal/hann_window.rs#L33-L63","documentation":"hann_window() computes a Hann window tensor of the requested length. Before building the tensor it converts the `size` (usize) to i64 via `i64::try_from(size)`, because the arange call needs i64. If `size` exceeds i64::MAX (only possible on 64-bit targets with an absurd value), the conversion fails and this expect() panics.","triggerScenarios":"Calling `Tensor::hann_window(size, ...)` (or `hann_window::hann_window`) with a size larger than i64::MAX (~9.2e18). On 32-bit platforms usize cannot exceed i64 range so it never fires; on 64-bit it requires an extreme or corrupted size value, e.g. one computed from bad math or a wrapped/negative value cast into usize.","commonSituations":"Almost always a bug in the caller's own size computation: multiplying window lengths, using a garbage config value, or a usize underflow (e.g. `0usize - 1`) producing a huge number passed as the window size.","solutions":["Verify the `size` argument passed to hann_window; print/log it before the call.","Check for usize arithmetic that can underflow or overflow before being passed as size (e.g. `a - b` where a < b, or multiplication overflow).","Clamp or validate size before calling, e.g. assert it is within a sane range such as < i64::MAX and realistically < a few million samples.","If a truly enormous window is intended, it exceeds practical memory anyway; redesign the algorithm (chunked/streaming windowing)."],"exampleFix":"// before\nlet size = samples_total - hop; // may underflow when hop > samples_total\nlet window = hann_window(size, &device);\n// after\nlet size = samples_total.checked_sub(hop).expect(\"hop must be <= samples_total\");\nassert!(size > 0 && size <= i64::MAX as usize);\nlet window = hann_window(size, &device);","handlingStrategy":"validation","validationCode":"fn safe_hann_window(size: usize, device: &Device) -> Tensor<1> {\n    assert!(size <= i64::MAX as usize, \"hann_window size must fit in i64\");\n    assert!(size > 0, \"hann_window size must be positive\");\n    hann_window(size, device)\n}","typeGuard":"fn fits_i64(v: usize) -> bool { v <= i64::MAX as usize }","tryCatchPattern":null,"preventionTips":["Validate window sizes come from checked arithmetic, not raw subtraction/multiplication on usize.","Use checked_sub/checked_mul when deriving sizes and reject None early.","Sanity-bound window length (e.g. < 1e8 samples) at the config-parsing layer.","Log the computed size before calling tensor APIs for easier debugging."],"tags":["rust","panic","integer-overflow","tensor","signal-processing"],"backgroundTag":"integer-overflow","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"}