9552 grep segfaults when you ask for context

Review Request #1090 — Created May 24, 2018 and submitted — Latest diff uploaded


You can specify the number of surrounding lines that should also be printed out for each match (with -B NUM, -C NUM or just -NUM) as context, but if you do grep with core dump. This is because the check that tests whether the buffer needs to grow is faulty. Due to missing parenthesis it causes the buffer to balloon prematurely and then later not grow when the buffer is no longer big enough, causing a buffer overflow. It's also possible that after growing it still won't be big enough so we need to do the check in a loop.

The stack trace wasn't much good (it was blowing up in memcpy) so I inserted a bunch of print statements to figure out what was wrong and then fixed the code. No segfaults anymore!