BurntSushi/ripgrep · error · ParseSizeError
invalid format for size '{}', which should be a non-empty se
Error message
invalid format for size '{}', which should be a non-empty sequence of digits followed by an optional 'K', 'M' or 'G' suffix What it means
parse_human_readable_size returns ParseSizeErrorKind::InvalidFormat (the 'format' constructor) when the input has no leading ASCII digits (digits.is_empty()) OR when the suffix after the digits is anything other than empty/K/M/G. The Display impl prints the expected grammar so end users know the accepted shapes.
Source
Thrown at crates/cli/src/human.rs:48
}
}
fn overflow(original: &str) -> ParseSizeError {
ParseSizeError {
original: original.to_string(),
kind: ParseSizeErrorKind::Overflow,
}
}
}
impl std::error::Error for ParseSizeError {}
impl std::fmt::Display for ParseSizeError {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
use self::ParseSizeErrorKind::*;
match self.kind {
InvalidFormat => write!(
f,
"invalid format for size '{}', which should be a non-empty \
sequence of digits followed by an optional 'K', 'M' or 'G' \
suffix",
self.original
),
InvalidInt(ref err) => write!(
f,
"invalid integer found in size '{}': {}",
self.original, err
),
Overflow => write!(f, "size too big in '{}'", self.original),
}
}
}
impl From<ParseSizeError> for std::io::Error {
fn from(size_err: ParseSizeError) -> std::io::Error {View on GitHub (pinned to 3fce3b5bb0)
Solutions
- Use one of the accepted forms: a non-empty digit run optionally followed by exactly K, M, or G (e.g. "512", "10M", "2G").
- Strip whitespace and reject decimals/signs before calling the parser, since neither is supported.
- If you need T/KB/MiB units, pre-normalize the value to bytes yourself before passing a plain byte count.
Example fix
// before
let n = parse_human_readable_size("1.5MiB")?; // errors: invalid format
// after
let n = parse_human_readable_size("1536M")?; // accepted Defensive patterns
Strategy: validation
Validate before calling
fn looks_like_size(s: &str) -> bool {
let mut it = s.bytes();
if !it.next().is_some_and(|b| b.is_ascii_digit()) { return false; }
let suffix: String = s.bytes().skip_while(|b| b.is_ascii_digit()).map(|b| b as char).collect();
suffix.is_empty() || matches!(suffix.as_str(), "K" | "M" | "G")
} Try / catch
let n = parse_human_readable_size(input);
if let Err(e) = n { eprintln!("invalid size: {e}"); return; } Prevention
- Document the accepted grammar (digits + optional single K/M/G) at every CLI flag that uses it.
- Pre-validate with a regex like ^[0-9]+[KMG]?$, before calling the parser.
- Normalize user input (trim whitespace, reject decimals/signs) upstream.
When it happens
Trigger: Calling parse_human_readable_size with "", "abc", "K", "-5", "12T", "12KB", "1.5M", or any string whose digit-prefix is empty or whose trailing suffix is unrecognized.
Common situations: CLI flag parsing where a user passes --max-filesize with the wrong unit (T, KB, MiB), a stray sign, a decimal point, or trailing whitespace; config files copied from a tool that uses different size syntax.
Related errors
- invalid integer found in size '{}': {}
- size too big in '{}'
- found invalid UTF-8 in pattern at byte offset {}: {} (disabl
- {}: {}
- {}:{}
AI-assisted analysis of BurntSushi/ripgrep@3fce3b5bb0 (2026-08-06).
Data as JSON: /data/errors/fb39d50a5e671d17.json.
Report an issue: GitHub.