{"record":{"id":"6cacbc4b63a78bbd","repo":"crossbeam-rs/crossbeam","slug":"bounded-channel-capacity-is-too-large","errorCode":null,"errorMessage":"bounded channel capacity is too large","messagePattern":"bounded channel capacity is too large","errorType":"panic","errorClass":null,"httpStatus":null,"severity":"error","filePath":"crossbeam-channel/src/flavors/array.rs","lineNumber":117,"sourceCode":"    mark_bit: Index,\n\n    /// Senders waiting while the channel is full.\n    senders: SyncWaker,\n\n    /// Receivers waiting while the channel is empty and not disconnected.\n    receivers: SyncWaker,\n}\n\nimpl<T> Channel<T> {\n    /// Creates a bounded channel of capacity `cap`.\n    pub(crate) fn with_capacity(cap: usize) -> Self {\n        assert!(cap > 0, \"capacity must be positive\");\n\n        // Use checked arithmetic before computing `mark_bit` and `one_lap`.\n        let mark_bit = (cap as Index)\n            .checked_add(1)\n            .and_then(Index::checked_next_power_of_two)\n            .expect(\"bounded channel capacity is too large\");\n        let one_lap = mark_bit\n            .checked_mul(2)\n            .expect(\"bounded channel capacity is too large\");\n\n        // Head is initialized to `{ lap: 0, mark: 0, index: 0 }`.\n        let head = 0;\n        // Tail is initialized to `{ lap: 0, mark: 0, index: 0 }`.\n        let tail = 0;\n\n        // Allocate a buffer of `cap` slots initialized\n        // with stamps.\n        let buffer: Box<[Slot<T>]> = (0..cap)\n            .map(|i| {\n                // Set the stamp to `{ lap: 0, mark: 0, index: i }`.\n                Slot {\n                    stamp: AtomicIndex::new(i as Index),\n                    msg: UnsafeCell::new(MaybeUninit::uninit()),\n                }","sourceCodeStart":99,"sourceCodeEnd":135,"githubUrl":"https://github.com/crossbeam-rs/crossbeam/blob/38dacb462261fcd64edcb308aed9cbf95c8c82c3/crossbeam-channel/src/flavors/array.rs#L99-L135","documentation":"The bounded (array-based) channel stores a mark bit and lap counter inside a single packed index word. The capacity plus one must fit as a power of two within the `Index` type; `checked_next_power_of_two`/`checked_mul` failing means the requested capacity is too large for the internal index representation, so the constructor panics instead of silently wrapping.","triggerScenarios":"Calling `bounded::<Index>(cap)` (e.g. `crossbeam_channel::bounded(cap)`) with a capacity so large that `cap + 1` rounded up to the next power of two, or that value times two, overflows the internal `Index` integer type.","commonSituations":"Passing `usize::MAX` or a user/CLI-supplied capacity without bounds; config value in bytes accidentally used as item count; computing capacity from a buffer size and overflowing on 32-bit platforms.","solutions":["Reduce the requested capacity to a value that fits the index type (practically, far below usize::MAX — e.g. cap at a few million)","Validate/clamp user- or config-supplied capacity before calling `bounded(cap)`","If huge capacity is truly needed, use an unbounded channel or a different data structure"],"exampleFix":"// before\nlet cap: usize = std::env::var(\"CAP\").unwrap().parse().unwrap();\nlet (tx, rx) = crossbeam_channel::bounded(cap); // may panic\n\n// after\nlet cap: usize = std::env::var(\"CAP\").unwrap().parse().unwrap();\nconst MAX_CAP: usize = 1 << 30;\nlet (tx, rx) = crossbeam_channel::bounded(cap.min(MAX_CAP).max(1));","handlingStrategy":"validation","validationCode":"const MAX_BOUNDED_CAP: usize = 1 << 30;\nassert!(cap > 0 && cap <= MAX_BOUNDED_CAP, \"capacity out of range\");\nlet (tx, rx) = crossbeam_channel::bounded(cap);","typeGuard":null,"tryCatchPattern":null,"preventionTips":["Clamp externally supplied capacities before constructing channels","Never use usize::MAX as capacity to mean 'unbounded' — use unbounded()","Test on smallest target platform (32-bit) if capacities are large"],"tags":["rust","crossbeam-channel","panic","capacity","overflow"],"backgroundTag":"value-out-of-range","analyzedSha":"38dacb462261fcd64edcb308aed9cbf95c8c82c3","analyzedAt":"2026-09-13T03:24:01.537Z","contentChangedAt":"2026-09-13T03:24:01.537Z","schemaVersion":2},"datasetVersion":"2026-09-15T23:17:13.987Z"}