rust-lang/cargo · error · io::Error

maximum limit reached when reading

Error message

maximum limit reached when reading

What it means

Raised by `LimitErrorReader` (src/util/io.rs:19), a wrapper around `std::io::Read::take(limit)`. When the underlying reader still has data (`read` returned `Ok(0)` specifically because `self.inner.limit() == 0`, i.e. the take budget is exhausted mid-stream) the reader returns an `io::Error` with kind `Other` and this message. It enforces a hard ceiling on how many bytes cargo will ingest from a source.

Source

Thrown at src/util/io.rs:19

use std::io::{self, Read, Take};

#[derive(Debug)]
pub struct LimitErrorReader<R> {
    inner: Take<R>,
}

impl<R: Read> LimitErrorReader<R> {
    pub fn new(r: R, limit: u64) -> LimitErrorReader<R> {
        LimitErrorReader {
            inner: r.take(limit),
        }
    }
}

impl<R: Read> Read for LimitErrorReader<R> {
    fn read(&mut self, buf: &mut [u8]) -> io::Result<usize> {
        match self.inner.read(buf) {
            Ok(0) if self.inner.limit() == 0 => Err(io::Error::new(
                io::ErrorKind::Other,
                "maximum limit reached when reading",
            )),
            e => e,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::LimitErrorReader;

    use std::io::Read;

    #[test]
    fn under_the_limit() {
        let buf = &[1; 7][..];
        let mut r = LimitErrorReader::new(buf, 8);

View on GitHub (pinned to 0e07a15537)

Solutions

  1. Identify the oversized source (the error surfaces during a download/extract) and verify it is the intended file; replace a corrupt/wrong file.
  2. Raise the applicable size limit if the large payload is legitimate (e.g. via the relevant config key or by using a different reader).
  3. For registry operators, ensure the published artifact is within expected bounds.

Example fix

// before
let mut r = LimitErrorReader::new(&data[..], 1024);
r.read_to_end(&mut out)?; // fails if data.len() > 1024
// after — size the limit to the real payload
let mut r = LimitErrorReader::new(&data[..], data.len() as u64);
r.read_to_end(&mut out)?;
Defensive patterns

Strategy: validation

Validate before calling

fn within_limit(len: u64, limit: u64) -> bool { len <= limit }
// before wrapping a reader, check the expected payload size against the cap:
// assert within_limit(metadata.len(), LIMIT)

Try / catch

// Read with a soft limit and react to LimitErrorReader errors explicitly
match r.read_to_end(&mut buf) {
    Err(e) if e.to_string().contains("maximum limit reached") => { /* truncate or reject */ }
    r => r,
}

Prevention

When it happens

Trigger: Reading a file/stream through `LimitErrorReader::new(r, limit)` where the source contains MORE than `limit` bytes. Once the take budget hits zero with data remaining, the next read yields this error rather than silently truncating.

Common situations: Downloading a `.crate` archive or index blob larger than the configured cap; a malicious/oversized registry response; a local file unexpectedly huge (corrupt or wrong file); cargo's download size limits engaged.

Related errors


AI-assisted analysis of rust-lang/cargo@0e07a15537 (2026-08-06). Data as JSON: /data/errors/1cc3e2f3ac1f6e03.json. Report an issue: GitHub.