From d6a0b3c9206201e865c6f2b3c5b304fb266df104 Mon Sep 17 00:00:00 2001 From: Zachary Dremann Date: Sun, 30 Jan 2022 17:15:29 -0500 Subject: [PATCH] Allow echo with escapes to work with \0 Testing with gecho on macos outputs a nul character for a \0 --- src/uu/echo/src/echo.rs | 5 +---- tests/by-util/test_echo.rs | 12 ++++++++++-- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/src/uu/echo/src/echo.rs b/src/uu/echo/src/echo.rs index 35606be71..1cb63fe93 100644 --- a/src/uu/echo/src/echo.rs +++ b/src/uu/echo/src/echo.rs @@ -88,10 +88,7 @@ fn print_escaped(input: &str, mut output: impl Write) -> io::Result { start = 0; next }), - '0' => parse_code(&mut iter, 8, 3, 3).unwrap_or_else(|| { - start = 0; - next - }), + '0' => parse_code(&mut iter, 8, 3, 3).unwrap_or('\0'), _ => { start = 0; next diff --git a/tests/by-util/test_echo.rs b/tests/by-util/test_echo.rs index 09ed9658f..401e16706 100644 --- a/tests/by-util/test_echo.rs +++ b/tests/by-util/test_echo.rs @@ -138,11 +138,19 @@ fn test_escape_short_octal() { } #[test] -fn test_escape_no_octal() { +fn test_escape_nul() { new_ucmd!() .args(&["-e", "foo\\0 bar"]) .succeeds() - .stdout_only("foo\\0 bar\n"); + .stdout_only("foo\0 bar\n"); +} + +#[test] +fn test_escape_octal_invalid_digit() { + new_ucmd!() + .args(&["-e", "foo\\08 bar"]) + .succeeds() + .stdout_only("foo\u{0}8 bar\n"); } #[test]