[Toybox] [PATCH] Fix grep to not truncate last character of file provided by -f argument

Daniel Mentz danielmentz at google.com
Mon Nov 7 14:26:30 PST 2022


The command line argument -f allows a user to specify a file that lists
multiple regular expressions (one per line) to match. It turned out
that, if the file ended with a new line character (like most text files
do), then the character just prior to that newline character got
truncated.

For example, if the file was just 4 bytes long and contained "abc\n",
then the character "c" would be removed.
---
 toys/posix/grep.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/toys/posix/grep.c b/toys/posix/grep.c
index 9b2e583c..5c6f5936 100644
--- a/toys/posix/grep.c
+++ b/toys/posix/grep.c
@@ -388,7 +388,7 @@ static void parse_regex(void)
       if (!*(s = xreadfile(al->arg, 0, 0))) {
         free(s);
         s = 0;
-      } else if (*(ss = s+strlen(s)-1)=='\n') *--ss = 0;
+      } else if (*(ss = s+strlen(s)-1)=='\n') *ss = 0;
     } else s = al->arg;
 
     // Advance, when we run out of -f switch to -e.
-- 
2.38.1.431.g37b22c650d-goog



More information about the Toybox mailing list