Skip to content

Commit b352f83

Browse files
Devel08pre-commit-ci[bot]cakebaker
authored
forbid scientific notation in numfmt #11655 (#11721)
* forbid scientific notation arguments in numfmt * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * Edit solution of #116555 * add scientific notation test * change variable name * use is_ascii_digit() instead of checking manually Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com> * Apply suggestion from @cakebaker Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com> --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com> Co-authored-by: Daniel Hofstetter <daniel.hofstetter@42dh.com>
1 parent ceacc67 commit b352f83

2 files changed

Lines changed: 13 additions & 1 deletion

File tree

src/uu/numfmt/src/numfmt.rs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,19 @@ fn format_and_write<W: std::io::Write>(
4949
None => input_line,
5050
};
5151

52+
// Return false if the input is in scientific notation
53+
if let Some(pos) = line.iter().position(|&b| b == b'E' || b == b'e') {
54+
if pos < line.len() - 1 {
55+
if line[pos + 1].is_ascii_digit() {
56+
let err = format!(
57+
"invalid suffix in input: '{}'",
58+
String::from_utf8_lossy(line)
59+
);
60+
return Err(Box::new(NumfmtError::FormattingError(err)));
61+
}
62+
}
63+
}
64+
5265
// In non-abort modes we buffer the formatted output so that on error we
5366
// can emit the original line instead.
5467
let buffer_output = !matches!(options.invalid, InvalidModes::Abort);

tests/by-util/test_numfmt.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1404,7 +1404,6 @@ fn test_large_integer_precision_loss_issue_11654() {
14041404
// uutils accepts scientific notation (`1e9`, `5e-3`, ...); GNU rejects it
14051405
// as "invalid suffix in input".
14061406
#[test]
1407-
#[ignore = "GNU compat: see uutils/coreutils#11655"]
14081407
fn test_scientific_notation_rejected_by_gnu_issue_11655() {
14091408
new_ucmd!()
14101409
.arg("1e9")

0 commit comments

Comments
 (0)