From dcbd350ccfe7eaa4feab038f97a382684f2adea9 Mon Sep 17 00:00:00 2001 From: Denis Vlasenko Date: Wed, 30 Apr 2008 00:39:22 +0000 Subject: echo: fix echo -e -n "msg\n\0" (by "Pinedo, David" ) --- coreutils/echo.c | 12 ++++++++---- testsuite/echo/echo-prints-slash-zero | 1 + 2 files changed, 9 insertions(+), 4 deletions(-) create mode 100644 testsuite/echo/echo-prints-slash-zero diff --git a/coreutils/echo.c b/coreutils/echo.c index 6e25db62c..cc9b9e6f4 100644 --- a/coreutils/echo.c +++ b/coreutils/echo.c @@ -108,15 +108,19 @@ int echo_main(int argc ATTRIBUTE_UNUSED, char **argv) } #if !ENABLE_FEATURE_FANCY_ECHO /* SUSv3 specifies that octal escapes must begin with '0'. */ - if ( (((unsigned char)*arg) - '1') >= 7) + if ( ((int)(unsigned char)(*arg) - '0') >= 8) /* '8' or bigger */ #endif { /* Since SUSv3 mandates a first digit of 0, 4-digit octals * of the form \0### are accepted. */ - if (*arg == '0' && ((unsigned char)(arg[1]) - '0') < 8) { - arg++; + if (*arg == '0') { + /* NB: don't turn "...\0" into "...\" */ + if (arg[1] && ((unsigned char)(arg[1]) - '0') < 8) { + arg++; + } } - /* bb_process_escape_sequence can handle nul correctly */ + /* bb_process_escape_sequence handles NUL correctly + * ("...\" case). */ c = bb_process_escape_sequence(&arg); } } diff --git a/testsuite/echo/echo-prints-slash-zero b/testsuite/echo/echo-prints-slash-zero new file mode 100644 index 000000000..d2466326f --- /dev/null +++ b/testsuite/echo/echo-prints-slash-zero @@ -0,0 +1 @@ +test "`busybox echo -e -n 'msg\n\0' | od -t x1 | head -n 1`" = "0000000 6d 73 67 0a 00" -- cgit v1.2.3