This is a follow-on from 310eefe, addressing the case where sscanf fails
and returns -1.
---
 lib/lib.c       | 2 +-
 tests/echo.test | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)
From 6bf45e58195427dd53bdadba69f506279d99dca1 Mon Sep 17 00:00:00 2001
From: Elliott Hughes <e...@google.com>
Date: Mon, 3 Aug 2020 13:38:46 -0700
Subject: [PATCH] echo: fix trailing \0.

This is a follow-on from 310eefe, addressing the case where sscanf fails
and returns -1.
---
 lib/lib.c       | 2 +-
 tests/echo.test | 2 ++
 2 files changed, 3 insertions(+), 1 deletion(-)

diff --git a/lib/lib.c b/lib/lib.c
index 930f66e4..03529a22 100644
--- a/lib/lib.c
+++ b/lib/lib.c
@@ -445,7 +445,7 @@ int unescape2(char **c, int echo)
   if (**c == 'c') return 31&*(++*c);
   for (i = 0; i<4; i++) {
     if (sscanf(*c, (char *[]){"0%3o%n"+!echo, "x%2x%n", "u%4x%n", "U%6x%n"}[i],
-        &idx, &off))
+        &idx, &off) > 0)
     {
       *c += off;
 
diff --git a/tests/echo.test b/tests/echo.test
index 012692ea..bd2c3ff7 100755
--- a/tests/echo.test
+++ b/tests/echo.test
@@ -49,3 +49,5 @@ testcmd "-eE" "-eE '\e'" '\\e\n' "" ""
 # This is how bash's built-in echo behaves, but now how /bin/echo behaves.
 toyonly testcmd "" "-e 'a\x123\ufb3bbc' | od -An -tx1" \
   " 61 12 33 ef ac bb 62 63 0a\n" "" ""
+
+testcmd "trailing nul" "-ne 'a\0b\0' | od -An -tx1" " 61 00 62 00\n" "" ""
-- 
2.28.0.163.g6104cc2f0b6-goog

_______________________________________________
Toybox mailing list
Toybox@lists.landley.net
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to