{"record":{"id":"8454d16ef48f6390","repo":"tokio-rs/tokio","slug":"provided-length-would-overflow-after-adjustment","errorCode":null,"errorMessage":"provided length would overflow after adjustment","messagePattern":"provided length would overflow after adjustment","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"tokio-util/src/codec/length_delimited.rs","lineNumber":547,"sourceCode":"                    LengthDelimitedCodecError { _priv: () },\n                ));\n            }\n\n            // The check above ensures there is no overflow\n            let n = n as usize;\n\n            // Adjust `n` with bounds checking\n            let n = if self.builder.length_adjustment < 0 {\n                n.checked_sub(-self.builder.length_adjustment as usize)\n            } else {\n                n.checked_add(self.builder.length_adjustment as usize)\n            };\n\n            // Error handling\n            match n {\n                Some(n) => n,\n                None => {\n                    return Err(io::Error::new(\n                        io::ErrorKind::InvalidInput,\n                        \"provided length would overflow after adjustment\",\n                    ));\n                }\n            }\n        };\n\n        src.advance(self.builder.get_num_skip());\n\n        // Ensure that the buffer has enough space to read the incoming\n        // payload\n        src.reserve(n.saturating_sub(src.len()));\n\n        Ok(Some(n))\n    }\n\n    fn decode_data(&self, n: usize, src: &mut BytesMut) -> Option<BytesMut> {\n        // At this point, the buffer has already had the required capacity","sourceCodeStart":529,"sourceCodeEnd":565,"githubUrl":"https://github.com/tokio-rs/tokio/blob/625954f365727668cb02d04172b34f1149637728/tokio-util/src/codec/length_delimited.rs#L529-L565","documentation":"Runtime error from `LengthDelimitedCodec::decode_head` (length_delimited.rs:547). After applying `length_adjustment` to the raw length-field value, `checked_add`/`checked_sub` returned `None` — the adjusted payload size overflowed `usize`. It indicates a `length_adjustment` configuration that pushes a representable length out of range.","triggerScenarios":"A length-field value that is valid on its own but, after the configured add/subtract of `length_adjustment`, exceeds `usize::MAX` or underflows. Almost always a builder misconfiguration rather than a runtime data condition.","commonSituations":"Wrong sign on `length_adjustment`; an adjustment value near `usize::MAX`; confusing the semantics of `length_adjustment` (it modifies the field-derived length, not `data.len()`).","solutions":["Re-check `length_adjustment` sign and magnitude against the protocol spec (`Builder::length_adjustment`).","Cap the wire length with `max_frame_length` so the adjustment cannot overflow.","Treat this as a configuration bug — fix the codec builder rather than handling it per-frame.","Print the builder settings (`length_field_len`, `length_field_offset`, `length_adjustment`, `num_skip`) and compare against the peer."],"exampleFix":"// before: wrong-sign adjustment causes overflow on large frames\nlet codec = LengthDelimitedCodec::builder()\n    .length_adjustment(-i64::MAX)\n    .new_codec();\n\n// after: correct adjustment per protocol spec\nlet codec = LengthDelimitedCodec::builder()\n    .length_field_length(4)\n    .length_adjustment(0)\n    .new_codec();","handlingStrategy":"validation","validationCode":"// Sanity-check the adjustment against the length field's representable range\nlet max_field = (1usize << (length_field_len * 8)) - 1;\ndebug_assert!(length_adjustment.abs() as u64 <= max_field as u64, \"adjustment can overflow the field\");","typeGuard":"fn is_length_overflow(e: &io::Error) -> bool {\n    e.kind() == io::ErrorKind::InvalidInput && e.to_string().contains(\"overflow after adjustment\")\n}","tryCatchPattern":"match framed.next().await {\n    Some(Err(e)) if is_length_overflow(&e) => { /* config bug: fix builder, then close */ return Err(anyhow!(\"length_adjustment misconfigured: {e}\")); }\n    other => other,\n}","preventionTips":["Treat this as a build-time configuration bug, not a runtime data condition.","Document the exact `length_adjustment` value next to the codec builder.","Validate all builder parameters in a unit test against the protocol spec."],"tags":["rust","tokio","tokio-util","codec","length-delimited","configuration","runtime"],"backgroundTag":null,"analyzedSha":"625954f365727668cb02d04172b34f1149637728","analyzedAt":"2026-08-11T17:46:45.378Z","contentChangedAt":"2026-08-11T17:46:45.378Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}