Skip to content

printf: trim leading whitespace when parsing numeric values #7512

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 40 additions & 3 deletions src/uucore/src/lib/features/format/num_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -181,11 +181,13 @@ impl ParsedNumber {
};
}

let trimmed_input = input.trim_ascii_start();

// Initial minus sign
let (negative, unsigned) = if let Some(input) = input.strip_prefix('-') {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not a big deal, but the lack of symmetry is not so nice, maybe.

I wonder if you want to do this intead:

let (negative, unsigned) = if let Some(trimmed_input) = trimmed_input.strip_prefix('-') {
            (true, trimmed_input)
} else {
            (false, trimmed_input)
}

Or just shadow input above:

let input = input.trim_ascii_start();

(true, input)
let (negative, unsigned) = if let Some(trimmed_input) = trimmed_input.strip_prefix('-') {
(true, trimmed_input)
} else {
(false, input)
(false, trimmed_input)
};

// Parse an optional base prefix ("0b" / "0B" / "0" / "0x" / "0X"). "0" is octal unless a
Expand Down Expand Up @@ -384,4 +386,39 @@ mod tests {
assert_eq!(Ok(0b1011), ParsedNumber::parse_u64("0b1011"));
assert_eq!(Ok(0b1011), ParsedNumber::parse_u64("0B1011"));
}

#[test]
fn test_parsing_with_leading_whitespace() {
assert_eq!(Ok(1), ParsedNumber::parse_u64(" 0x1"));
assert_eq!(Ok(-2), ParsedNumber::parse_i64(" -0x2"));
assert_eq!(Ok(-3), ParsedNumber::parse_i64(" \t-0x3"));
assert_eq!(Ok(-4), ParsedNumber::parse_i64(" \n-0x4"));
assert_eq!(Ok(-5), ParsedNumber::parse_i64(" \n\t\u{000d}-0x5"));

// Ensure that trailing whitespace is still a partial match
assert_eq!(
Err(ParseError::PartialMatch(6, " ")),
ParsedNumber::parse_u64("0x6 ")
);
assert_eq!(
Err(ParseError::PartialMatch(7, "\t")),
ParsedNumber::parse_u64("0x7\t")
);
assert_eq!(
Err(ParseError::PartialMatch(8, "\n")),
ParsedNumber::parse_u64("0x8\n")
);

// Ensure that unicode non-ascii whitespace is a partial match
assert_eq!(
Err(ParseError::NotNumeric),
ParsedNumber::parse_i64("\u{2029}-0x9")
);

// Ensure that whitespace after the number has "started" is not allowed
assert_eq!(
Err(ParseError::NotNumeric),
ParsedNumber::parse_i64("- 0x9")
);
}
}
26 changes: 26 additions & 0 deletions tests/by-util/test_printf.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1053,3 +1053,29 @@ fn float_switch_switch_decimal_scientific() {
.succeeds()
.stdout_only("1e-05");
}

#[test]
fn float_arg_with_whitespace() {
new_ucmd!()
.args(&["%f", " \u{0020}\u{000d}\t\n0.000001"])
.succeeds()
.stdout_only("0.000001");

new_ucmd!()
.args(&["%f", "0.1 "])
.fails()
.stderr_contains("value not completely converted");

// Unicode whitespace should not be allowed in a number
new_ucmd!()
.args(&["%f", "\u{2029}0.1"])
.fails()
.stderr_contains("expected a numeric value");
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Coming from that interesting discovery, do you want to test that printf doesn't actually expand \t macro?

i.e. .args(&["%f", "\\t0.1"]) fails. You'll need to add a comment to explain too, I'm sure others will get confused about this one too.


// A input string with a whitespace special character that has
// not already been expanded should fail.
new_ucmd!()
.args(&["%f", "\\t0.1"])
.fails()
.stderr_contains("expected a numeric value");
}
Loading