diff --git a/src/uu/factor/src/factor.rs b/src/uu/factor/src/factor.rs index 898679893c9..3fe461e016d 100644 --- a/src/uu/factor/src/factor.rs +++ b/src/uu/factor/src/factor.rs @@ -28,7 +28,8 @@ fn write_factors_str( w: &mut io::BufWriter, print_exponents: bool, ) -> UResult<()> { - let rx = num_str.trim().parse::(); + let trimmed = num_str.trim_matches(|c: char| c.is_whitespace() || c == '\0'); + let rx = trimmed.parse::(); let Ok(x) = rx else { // return Ok(). it's non-fatal and we should try the next number. show_warning!("{}: {}", num_str.maybe_quote(), rx.unwrap_err()); diff --git a/tests/by-util/test_factor.rs b/tests/by-util/test_factor.rs index 79c038bdab4..d4360bd7110 100644 --- a/tests/by-util/test_factor.rs +++ b/tests/by-util/test_factor.rs @@ -54,6 +54,15 @@ fn test_repeated_exponents() { .no_stderr(); } +#[test] +fn test_trim_null_chars() { + new_ucmd!() + .pipe_in("42\0") + .succeeds() + .stdout_only("42: 2 3 7\n") + .no_stderr(); +} + #[test] #[cfg(feature = "sort")] #[cfg(not(target_os = "android"))]