{"record":{"id":"920c338c15ab72d4","repo":"rayon-rs/rayon","slug":"overflow","errorCode":null,"errorMessage":"overflow","messagePattern":"overflow","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"src/iter/chain.rs","lineNumber":72,"sourceCode":"}\n\nimpl<A, B> IndexedParallelIterator for Chain<A, B>\nwhere\n    A: IndexedParallelIterator,\n    B: IndexedParallelIterator<Item = A::Item>,\n{\n    fn drive<C>(self, consumer: C) -> C::Result\n    where\n        C: Consumer<Self::Item>,\n    {\n        let Chain { a, b } = self;\n        let (left, right, reducer) = consumer.split_at(a.len());\n        let (a, b) = join(|| a.drive(left), || b.drive(right));\n        reducer.reduce(a, b)\n    }\n\n    fn len(&self) -> usize {\n        self.a.len().checked_add(self.b.len()).expect(\"overflow\")\n    }\n\n    fn with_producer<CB>(self, callback: CB) -> CB::Output\n    where\n        CB: ProducerCallback<Self::Item>,\n    {\n        let a_len = self.a.len();\n        return self.a.with_producer(CallbackA {\n            callback,\n            a_len,\n            b: self.b,\n        });\n\n        struct CallbackA<CB, B> {\n            callback: CB,\n            a_len: usize,\n            b: B,\n        }","sourceCodeStart":54,"sourceCodeEnd":90,"githubUrl":"https://github.com/rayon-rs/rayon/blob/ee0a00bdb1ab039e178a215ad5712fb7fa58e58f/src/iter/chain.rs#L54-L90","documentation":"`Chain`'s `ExactSizeIterator::len()` adds the lengths of both inner iterators with `checked_add` and panics on usize overflow. The combined parallel iterator claims to be longer than `usize::MAX`, which cannot be represented.","triggerScenarios":"`chain(a, b).len()` (or any indexed parallel op on it) where `a.len() + b.len()` overflows usize — only feasible with extreme exact-size iterators (e.g. huge ranges with large item strides like `(0..usize::MAX).step_by(k)`).","commonSituations":"Constructing iterators over near-usize::MAX elements and chaining them; synthetic/benchmark iterators with huge lengths.","solutions":["Avoid chaining iterators whose combined length approaches usize::MAX","Reduce range sizes or step strides before chaining","Use `chain` on iterators with exact but small lengths, or unindexed sources","Check lengths first: if `a.len() > usize::MAX - b.len()`, restructure"],"exampleFix":"// before\nlet it = (0..usize::MAX).step_by(2).chain((0..usize::MAX).step_by(2));\n// after\nlet it = (0..usize::MAX / 4).step_by(2).chain((0..usize::MAX / 4).step_by(2));","handlingStrategy":"validation","validationCode":"fn chain_fits(a: usize, b: usize) -> bool { a.checked_add(b).is_some() }","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Check checked_add of lengths before chaining huge iterators","Keep exact-size iterator lengths well below usize::MAX","Use unindexed iterators for extreme sizes"],"tags":["rayon","panic","overflow","iterator"],"backgroundTag":"integer-overflow","analyzedSha":"ee0a00bdb1ab039e178a215ad5712fb7fa58e58f","analyzedAt":"2026-09-07T23:28:47.588Z","contentChangedAt":"2026-09-07T23:28:47.588Z","schemaVersion":2},"datasetVersion":"2026-09-16T04:17:20.429Z"}