Skip to content
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
11 changes: 7 additions & 4 deletions src/uu/factor/src/factor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,22 +81,25 @@ fn parse_num(slice: &[u8]) -> UResult<Number> {
)
};
let num = str::from_utf8(slice).map_err(|_| err_invalid(&NumError(slice).to_string()))?;
// Only leading spaces are skipped; other whitespace makes the number invalid.
let digits = num.trim_start_matches(' ');

match num.parse::<u64>() {
match digits.parse::<u64>() {
Ok(x) => return Ok(Number::U64(x)),
// If overflown, attempt a greater width
Err(e) if *e.kind() == IntErrorKind::PosOverflow => {}
Err(_) => return Err(err_invalid(num)),
}

match num.parse::<u128>() {
match digits.parse::<u128>() {
Ok(x) => return Ok(Number::U128(x)),
// If overflown, attempt a greater width
Err(e) if *e.kind() == IntErrorKind::PosOverflow => {}
Err(_) => return Err(err_invalid(num)),
}

num.parse::<BigUint>()
digits
.parse::<BigUint>()
.map(Number::BigUint)
.map_err(|_| err_invalid(num))
}
Expand Down Expand Up @@ -159,7 +162,7 @@ pub fn uumain(args: impl uucore::Args) -> UResult<()> {

if let Some(values) = matches.get_many::<String>(options::NUMBER) {
for number in values {
write_factors_str(number.trim().as_bytes(), &mut w, print_exponents)?;
write_factors_str(number.as_bytes(), &mut w, print_exponents)?;
}
} else {
let stdin = stdin();
Expand Down
17 changes: 17 additions & 0 deletions tests/by-util/test_factor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,23 @@ fn test_repeated_exponents() {
.no_stderr();
}

#[test]
fn test_argument_whitespace() {
new_ucmd!()
.args(&[" 30", " +7"])
.succeeds()
.stdout_only("30: 2 3 5\n7: 7\n");
new_ucmd!()
.args(&["30 ", " x"])
.fails_with_code(1)
.no_stdout()
.stderr_is(
"factor: '30 ' is not a valid positive integer\n\
factor: ' x' is not a valid positive integer\n",
);
new_ucmd!().arg("\t30").fails_with_code(1).no_stdout();
}

#[test]
fn test_trim_null_chars() {
new_ucmd!()
Expand Down
Loading