From f52e3e800bb5bd429f10c34cb86b997e33a670e0 Mon Sep 17 00:00:00 2001 From: Yotam Medini Date: Wed, 4 Mar 2026 10:54:17 +0200 Subject: [PATCH] factor: trim also null-chars (#11182) * fix bug #11097: factor: trim also null-chars * for factor: add test_trim_null_chars * test_factor: pipe_in instead of pipe_in_fixture --- src/uu/factor/src/factor.rs | 3 ++- tests/by-util/test_factor.rs | 9 +++++++++ 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/src/uu/factor/src/factor.rs b/src/uu/factor/src/factor.rs index 898679893..3fe461e01 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 79c038bda..d4360bd71 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"))]