{"record":{"id":"1cc3e2f3ac1f6e03","repo":"rust-lang/cargo","slug":"maximum-limit-reached-when-reading","errorCode":null,"errorMessage":"maximum limit reached when reading","messagePattern":"maximum limit reached when reading","errorType":"exception","errorClass":"io::Error","httpStatus":null,"severity":"error","filePath":"src/util/io.rs","lineNumber":19,"sourceCode":"use std::io::{self, Read, Take};\n\n#[derive(Debug)]\npub struct LimitErrorReader<R> {\n    inner: Take<R>,\n}\n\nimpl<R: Read> LimitErrorReader<R> {\n    pub fn new(r: R, limit: u64) -> LimitErrorReader<R> {\n        LimitErrorReader {\n            inner: r.take(limit),\n        }\n    }\n}\n\nimpl<R: Read> Read for LimitErrorReader<R> {\n    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {\n        match self.inner.read(buf) {\n            Ok(0) if self.inner.limit() == 0 => Err(io::Error::new(\n                io::ErrorKind::Other,\n                \"maximum limit reached when reading\",\n            )),\n            e => e,\n        }\n    }\n}\n\n#[cfg(test)]\nmod tests {\n    use super::LimitErrorReader;\n\n    use std::io::Read;\n\n    #[test]\n    fn under_the_limit() {\n        let buf = &[1; 7][..];\n        let mut r = LimitErrorReader::new(buf, 8);","sourceCodeStart":1,"sourceCodeEnd":37,"githubUrl":"https://github.com/rust-lang/cargo/blob/eb98b54bc9f3c74519f43d066cb3fd02ebc88df0/src/util/io.rs#L1-L37","documentation":"`LimitErrorReader` wraps an inner reader with `take(limit)`, capping total bytes read. On each `read`, if the underlying read returns `Ok(0)` AND the remaining limit has hit `0` (`self.inner.limit() == 0`), the reader returns an `io::Error` (kind `Other`, message `\"maximum limit reached when reading\"`) instead of a clean EOF. This converts a silent truncation into an explicit error so callers cannot mistake a size-limited stream for a complete one.","triggerScenarios":"Reading from a `LimitErrorReader` past its configured byte budget: the inner `take(limit)` exhausts the limit, the next `read` yields `Ok(0)`, and `limit() == 0` — so the reader signals the cap as an error rather than EOF.","commonSituations":"Parsing a downloaded file (e.g. crate `.crate` archive, registry index, lockfile) where Cargo imposes a size guard against pathologically large inputs; an upstream server returning a payload larger than the configured cap; or a corrupted/truncated download that exceeds an expected bound.","solutions":["Increase the configured read limit if the input is legitimately large and trusted.","Verify the upstream source is not returning an unexpectedly large or malicious payload.","Ensure the stream is not being read twice or chained in a way that double-counts bytes against the limit."],"exampleFix":"// before\nlet mut r = LimitErrorReader::new(source, 1024 * 1024);\n// reading >1MiB -> Err(\"maximum limit reached when reading\")\n\n// after\nlet mut r = LimitErrorReader::new(source, 64 * 1024 * 1024);","handlingStrategy":"try-catch","validationCode":"// before wrapping, confirm the expected size is within the budget\nfn within_budget(expected: u64, limit: u64) -> Result<(), String> {\n    if expected > limit { return Err(format!(\"expected {expected} exceeds limit {limit}\")); }\n    Ok(())\n}","typeGuard":null,"tryCatchPattern":"match reader.read_to_end(&mut buf) {\n    Ok(n) => { /* complete read within limit */ }\n    Err(e) if e.to_string().contains(\"maximum limit reached when reading\") => {\n        // handle truncated/oversized input: raise the limit or reject the source\n    }\n    Err(e) => return Err(e.into()),\n}","preventionTips":["Size the LimitErrorReader budget to the largest legitimate input.","Validate Content-Length / expected size before streaming when possible.","Treat a limit hit as a security/trust signal for untrusted sources."],"tags":["io","reader","limit","size-cap","cargo"],"backgroundTag":null,"analyzedSha":"eb98b54bc9f3c74519f43d066cb3fd02ebc88df0","analyzedAt":"2026-08-11T17:42:36.556Z","contentChangedAt":"2026-08-11T17:42:36.556Z","schemaVersion":2},"datasetVersion":"2026-09-23T08:17:48.524Z"}