From 20e09d90968ee422c33a30cba563cf7af0e21dcc Mon Sep 17 00:00:00 2001 From: huuan26 Date: Tue, 3 Nov 2026 13:36:30 +0700 Subject: [PATCH] od: report current offset for empty strings with --strings=0 (#16134) --- src/uu/od/src/od.rs | 9 +++++++-- tests/by-util/test_od.rs | 19 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/src/uu/od/src/od.rs b/src/uu/od/src/od.rs index 11eb24a5ec1..2b5d1c2b7f0 100644 --- a/src/uu/od/src/od.rs +++ b/src/uu/od/src/od.rs @@ -632,7 +632,7 @@ fn extract_strings_from_input( }; let mut current_string = Vec::new(); - let mut string_start_offset = 0u64; + let mut string_start_offset = skip_bytes; let mut current_offset = skip_bytes; let mut bytes_read = 0u64; let mut buf = [0u8; 1]; @@ -665,7 +665,12 @@ fn extract_strings_from_input( // Either null terminator or non-printable character if byte == 0 && current_string.len() >= min_length { // Null terminator found with valid string - print_string(string_start_offset, ¤t_string)?; + let offset = if current_string.is_empty() { + current_offset + } else { + string_start_offset + }; + print_string(offset, ¤t_string)?; } current_string.clear(); } diff --git a/tests/by-util/test_od.rs b/tests/by-util/test_od.rs index 699a0ef1ef6..5ebd456ce3d 100644 --- a/tests/by-util/test_od.rs +++ b/tests/by-util/test_od.rs @@ -1390,6 +1390,25 @@ fn test_od_strings_option() { .succeeds() .stdout_is("0000000 hello\n0000006 world\n"); + // Test -S0 with empty strings and consecutive NUL bytes (issue #16134) + new_ucmd!() + .args(&["--strings=0", "-Ad"]) + .pipe_in(b"abc\x00\x00def\x00") + .succeeds() + .stdout_is("0000000 abc\n0000004 \n0000005 def\n"); + + new_ucmd!() + .args(&["-S0", "-Ad"]) + .pipe_in(b"\x00\x00") + .succeeds() + .stdout_is("0000000 \n0000001 \n"); + + new_ucmd!() + .args(&["--strings=0", "-Ad", "-j4"]) + .pipe_in(b"abc\x00\x00def\x00") + .succeeds() + .stdout_is("0000004 \n0000005 def\n"); + // Test -S0 with single character strings new_ucmd!() .arg("-S0")