mirror of
https://github.com/uutils/coreutils.git
synced 2026-06-10 15:48:22 -07:00
fix(numfmt): Read lines only up to null byte (as GNU does)
This commit is contained in:
committed by
Daniel Hofstetter
parent
7e8c1ba472
commit
f197618f7d
@@ -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")),
|
||||
}
|
||||
|
||||
@@ -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");
|
||||
}
|
||||
Reference in New Issue
Block a user