From 3ad73e1344afa7812671d08456591b8cde952775 Mon Sep 17 00:00:00 2001 From: William Haddon Date: Sat, 9 Nov 2013 19:37:41 -0600 Subject: 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. --- toys/posix/grep.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) (limited to 'toys/posix/grep.c') 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:]])("); -- cgit v1.2.3