Skip to content

head --bytes=0K outputs up to 1 KiB instead of zero bytes #13703

Description

@SichangHe

Doing

printf 0123456789 | cargo r -- head --bytes=0K

gives

0123456789

But it should instead print nothing.

It seems the code passes 0K through the function below, removes every initial 0, turning 0K into bare K.

pub fn parse_signed_num_max(src: &str) -> Result<SignedNum, ParseSizeError> {
let (sign, size_string) = strip_sign_prefix(src);
// Empty string after stripping sign is an error
if size_string.is_empty() {
return Err(ParseSizeError::ParseFailure(src.to_string()));
}
// Remove leading zeros so size is interpreted as decimal, not octal
let trimmed = size_string.trim_start_matches('0');
let value = if trimmed.is_empty() {
// All zeros (e.g., "000" or "0")
0
} else {
parse_size_u64_max(trimmed)?
};
Ok(SignedNum { value, sign })
}

Then, the bare K is interpreted as 1K.

The fix can simply be to check if any zeros were removed before interpreting K to 1K.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions