aboutsummaryrefslogtreecommitdiff
path: root/toys/posix/grep.c
diff options
context:
space:
mode:
authorWilliam Haddon <william@haddonthethird.net>2013-11-09 19:37:41 -0600
committerWilliam Haddon <william@haddonthethird.net>2013-11-09 19:37:41 -0600
commit3ad73e1344afa7812671d08456591b8cde952775 (patch)
tree7b9ef2059e453b639dde51f704ed754a045b2177 /toys/posix/grep.c
parentaa2b8abf8efff6da663382735faf519c131d5a68 (diff)
downloadtoybox-3ad73e1344afa7812671d08456591b8cde952775.tar.gz
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.
Diffstat (limited to 'toys/posix/grep.c')
-rw-r--r--toys/posix/grep.c3
1 files changed, 2 insertions, 1 deletions
diff --git a/toys/posix/grep.c b/toys/posix/grep.c
index 4338a552..8877f452 100644
--- a/toys/posix/grep.c
+++ b/toys/posix/grep.c
@@ -212,7 +212,8 @@ static void parse_regex(void)
// 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:]])(");