{"record":{"id":"1a266878e835ef98","repo":"tracel-ai/burn","slug":"blackmanwindow-size-doesn-t-fit-in-i64-range","errorCode":null,"errorMessage":"BlackmanWindow size doesn't fit in i64 range.","messagePattern":"BlackmanWindow size doesn't fit in i64 range\\.","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crates/burn-tensor/src/tensor/signal/blackman_window.rs","lineNumber":71,"sourceCode":"pub fn blackman_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>(\"BlackmanWindow\", &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(\"BlackmanWindow 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    let cos_val = Tensor::<1, Int>::arange(0..size_i64, &opt.device)\n        .float()\n        .mul_scalar(angular_increment)\n        .cos();\n\n    // Using the double angle property of cosine: cos(2θ) = 2cos^2(θ) - 1\n    // w[n] = 0.42 - 0.5cos(2πn / N) + 0.08cos(4πn / N)\n    // w[n] = 0.42 - 0.5cos(2πn / N) + 0.08cos(2 * (2πn / N))\n    // w[n] = 0.42 - 0.5cos(2πn / N) + 0.08(2cos^2(2πn / N) - 1)\n    // w[n] = 0.34 - 0.5cos(2πn / N) + 0.16cos^2(2πn / N)\n    let first_cos_term = cos_val.clone().mul_scalar(-0.5);\n    let second_cos_term = cos_val.powi_scalar(2).mul_scalar(0.16);\n    first_cos_term\n        .add(second_cos_term)\n        .add_scalar(0.34)\n        .cast(dtype)","sourceCodeStart":53,"sourceCodeEnd":89,"githubUrl":"https://github.com/tracel-ai/burn/blob/d16f7ba2ed0d41408189384044cc886fb4c8f957/crates/burn-tensor/src/tensor/signal/blackman_window.rs#L53-L89","documentation":"blackman_window builds the window by materializing an arange(0..size) Int tensor whose backing values are i64; if the requested window size cannot be represented in i64, i64::try_from fails and the expect panics. This is a defensive overflow check for absurd sizes.","triggerScenarios":"Calling `blackman_window(size)` where size > i64::MAX; computing the size from an overflowed/incorrect arithmetic expression (e.g. usize arithmetic on 32-bit-units or a wrapped counter).","commonSituations":"Signal-processing configs where the window length is derived from sample counts that overflow; test/fuzz inputs passing usize::MAX; misconfigured FFT length parameters.","solutions":["Ensure the requested window size is a sane value (< i64::MAX) before calling","Validate the size in caller code (e.g. cap at a practical limit like 2^32)","Fix upstream arithmetic that computes the size to avoid overflow/wrapping","Note the early-return path: size == 1 is handled separately, so sizes >= 2 within i64 are safe"],"exampleFix":"// before\nlet w = blackman_window(size, periodic, device);\n// after\nassert!(size <= i64::MAX as usize, \"window size too large\");\nlet w = blackman_window(size, periodic, device);","handlingStrategy":"validation","validationCode":"if size > (i64::MAX as usize) {\n    return Err(\"Blackman window size too large\".into());\n}\nlet w = blackman_window(size, periodic, device);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp window sizes to practical limits at config parsing","Use checked arithmetic when deriving size from sample counts","Fuzz-test window-size parameters for overflow"],"tags":["signal","overflow","panic","burn-tensor"],"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"}