diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/path_list_editor.rs | 15 |
1 files changed, 12 insertions, 3 deletions
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<R: Read, W: Write>( @@ -36,7 +35,7 @@ pub fn append_paths<R: Read, W: Write>( ) -> 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 { @@ -219,6 +218,16 @@ mod tests { } #[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""; let mut output = Vec::new(); |
