{"record":{"id":"c6d1d782196e6cfc","repo":"tokio-rs/tokio","slug":"frame-size-too-big","errorCode":null,"errorMessage":"frame size too big","messagePattern":"frame size too big","errorType":"exception","errorClass":"LengthDelimitedCodecError","httpStatus":null,"severity":"error","filePath":"tokio-util/src/codec/length_delimited.rs","lineNumber":527,"sourceCode":"            // Not enough data\n            return Ok(None);\n        }\n\n        let n = {\n            let mut src = Cursor::new(&mut *src);\n\n            // Skip the required bytes\n            src.advance(self.builder.length_field_offset);\n\n            // match endianness\n            let n = if self.builder.length_field_is_big_endian {\n                src.get_uint(field_len)\n            } else {\n                src.get_uint_le(field_len)\n            };\n\n            if n > self.builder.max_frame_len as u64 {\n                return Err(io::Error::new(\n                    io::ErrorKind::InvalidData,\n                    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,","sourceCodeStart":509,"sourceCodeEnd":545,"githubUrl":"https://github.com/tokio-rs/tokio/blob/625954f365727668cb02d04172b34f1149637728/tokio-util/src/codec/length_delimited.rs#L509-L545","documentation":"Runtime error from `LengthDelimitedCodec::decode_head` (length_delimited.rs:527). The length value read from the wire's length field exceeds the configured `max_frame_len`; it returns `LengthDelimitedCodecError` as `io::ErrorKind::InvalidData`. This guard bounds memory use against a bogus/hostile length header.","triggerScenarios":"The decoded length-field value is larger than `max_frame_length()`. Causes: peer sends a frame larger than the local cap; corrupt bytes; wrong `length_field_len`/`length_field_offset`/endianness so garbage is read as the length; adversarial input.","commonSituations":"Peer and local disagree on max frame size; builder misconfiguration (wrong endianness, wrong field width, wrong offset) making a small frame look huge; forgot to raise `max_frame_length` for a protocol with large frames; malicious/untrusted peer.","solutions":["If frames are legitimately large, raise the cap: `LengthDelimitedCodec::builder().max_frame_length(N)`.","Verify `length_field_len`, `length_field_offset`, and `length_field_is_big_endian` match the peer's wire format.","Treat the error as a protocol violation and close the connection when reading from an untrusted peer.","Always set a sane `max_frame_length` to bound buffer growth."],"exampleFix":"// before: default 8MB cap rejects a 16MB frame\nlet codec = LengthDelimitedCodec::new();\n\n// after: raise the cap to match the protocol\nlet codec = LengthDelimitedCodec::builder()\n    .max_frame_length(16 * 1024 * 1024)\n    .new_codec();","handlingStrategy":"validation","validationCode":"// Validate builder settings before use\nlet max = codec.max_frame_length();\ndebug_assert!(max >= protocol_max_frame, \"codec max smaller than protocol allows\");\nassert!(length_field_len <= 8 && length_field_offset + length_field_len <= header_len);","typeGuard":"fn is_frame_too_big(e: &io::Error) -> bool {\n    e.kind() == io::ErrorKind::InvalidData && e.get_ref().map(|x| x.is::<LengthDelimitedCodecError>()).unwrap_or(false)\n}","tryCatchPattern":"match framed.next().await {\n    Some(Err(e)) if is_frame_too_big(&e) => { close_peer().await; continue; }\n    Some(Err(e)) => return Err(e.into()),\n    Some(Ok(f)) => handle(f),\n    None => break,\n}","preventionTips":["Agree on and set `max_frame_length` identically on both peers.","Triple-check `length_field_len`, `length_field_offset`, and endianness against the spec.","Always bound `max_frame_length` when reading from untrusted peers."],"tags":["rust","tokio","tokio-util","codec","length-delimited","network","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"}