Skip to main content

read_bytes_capped

Function read_bytes_capped 

Source
pub fn read_bytes_capped(path: &Path, max_bytes: u64) -> Result<Vec<u8>>
Expand description

Reads a file as bytes, refusing anything larger than max_bytes.

Byte-oriented twin of read_text_capped for content that is not required to be UTF-8 — PEM material is base64 in practice but its DER neighbours are not, and rejecting a valid certificate over an encoding assumption would be a worse failure than the one this cap exists to prevent.

The size is checked twice on purpose: metadata can race with a writer, so the take provides the guarantee and the up-front check merely avoids opening a file that is already known to be too large.

§Errors

std::io::ErrorKind::InvalidData when the file exceeds max_bytes; any other I/O error from metadata, open or the read itself.