From ea5c36fff4922c06181e1942d7db36ebb96cfd1b Mon Sep 17 00:00:00 2001 From: A Farzat Date: Sat, 20 Jun 2026 20:14:29 +0300 Subject: Make append_paths preserve stream if no additions --- src/path_list_editor.rs | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/path_list_editor.rs b/src/path_list_editor.rs index 1ae4816..d605d25 100644 --- a/src/path_list_editor.rs +++ b/src/path_list_editor.rs @@ -25,8 +25,7 @@ const NUL: &[u8] = b"\0"; /// /// This function is defensive about unterminated input: if `input` is non-empty and does not end /// with a NUL byte, a NUL byte is inserted before appended paths are written. If `new_paths` is -/// empty, this still has the effect of repairing a non-empty unterminated input stream into a -/// NUL-terminated one. +/// empty, the input stream is passed through unchanged. /// /// This function does not normalize, deduplicate, or validate paths. pub fn append_paths( @@ -36,7 +35,7 @@ pub fn append_paths( ) -> std::io::Result<()> { let last_byte_in_input = copy_while_tracking_last_byte(input, &mut output)?; // Defensively ensure non-empty input is NUL-terminated. - if matches!(last_byte_in_input, Some(byte) if byte != 0) { + if !new_paths.is_empty() && matches!(last_byte_in_input, Some(byte) if byte != 0) { output.write_all(NUL)?; } for path in new_paths { @@ -218,6 +217,16 @@ mod tests { assert_eq!(output, b"a.rs\0b.rs\0c.rs\0"); } + #[test] + fn append_paths_does_not_insert_nul_when_no_new_paths_and_input_not_terminated() { + let input = b"a.rs"; + let mut output = Vec::new(); + + append_paths(&input[..], &mut output, &[]).unwrap(); + + assert_eq!(output, b"a.rs"); + } + #[test] fn prepend_paths_works_with_empty_stdin_and_args() { let input = b""; -- cgit v1.3.1