changeset 1110:241ee03473db draft

grep doesn't allocate enough space Grep miscalculates the amount of memory it needs to allocate when "converting strings to one big regex" when the -e flag is not specified. Since in this case "\|" is inserted between strings rather than "|", two extra bytes rather than one need to be provided for each string. I noticed this because it caused grep to seg-fault on musl when a regex of exactly seven characters is provided.
author William Haddon <william@haddonthethird.net>
date Sat, 09 Nov 2013 19:37:41 -0600
parents 060217c83f0f
children f665f065fe87
files toys/posix/grep.c
diffstat 1 files changed, 2 insertions(+), 1 deletions(-) [+]
line wrap: on
line diff
--- a/toys/posix/grep.c	Sat Nov 09 12:23:48 2013 -0600
+++ b/toys/posix/grep.c	Sat Nov 09 19:37:41 2013 -0600
@@ -212,7 +212,8 @@
 
     // Convert strings to one big regex
     if (w) len = 36;
-    for (al = TT.e; al; al = al->next) len += strlen(al->arg)+1;
+    for (al = TT.e; al; al = al->next)
+      len += strlen(al->arg)+1+!(toys.optflags & FLAG_E);
 
     regstr = s = xmalloc(len);
     if (w) s = stpcpy(s, "(^|[^_[:alnum:]])(");