fix(numfmt): Read lines only up to null byte (as GNU does)

This commit is contained in:
FidelSch
2026-03-02 10:34:17 +01:00
committed by Daniel Hofstetter
parent 7e8c1ba472
commit f197618f7d
2 changed files with 30 additions and 2 deletions
+9 -2
View File
@@ -72,11 +72,18 @@ fn write_line<W: std::io::Write>(
input_line: &[u8],
options: &NumfmtOptions,
) -> UResult<()> {
// Read lines only up to null byte (as GNU does)
let line = input_line
.iter()
.take_while(|&&b| b != b'\0')
.cloned()
.collect::<Vec<u8>>();
let handled_line = if options.delimiter.is_some() {
write_formatted_with_delimiter(writer, input_line, options)
write_formatted_with_delimiter(writer, &line, options)
} else {
// Whitespace mode requires valid UTF-8
match std::str::from_utf8(input_line) {
match std::str::from_utf8(&line) {
Ok(s) => write_formatted_with_whitespace(writer, s, options),
Err(_) => Err(translate!("numfmt-error-invalid-input")),
}
+21
View File
@@ -1249,3 +1249,24 @@ fn test_empty_delimiter_whitespace_rejection() {
.fails_with_code(2)
.stderr_contains("invalid suffix in input");
}
#[test]
fn test_null_byte_input() {
new_ucmd!()
.pipe_in("1000\x00")
.succeeds()
.stdout_is("1000\n");
}
#[test]
fn test_null_byte_input_multiline() {
new_ucmd!()
.pipe_in("1000\x00\n2000\x00")
.succeeds()
.stdout_is("1000\n2000\n");
new_ucmd!()
.pipe_in("1000\x002000\n3000")
.succeeds()
.stdout_is("1000\n3000\n");
}