Fix infinite loop when parsing very long results

If the result reason is big enough and it contains newlines, the expanded
version that we construct by replacing '\n' with '<<NEWLINE>>' triggers a
bug that causes our algorithm to get stuck in an infinite loop.  This is
because fgets() with a 1-byte long buffer won't ever return any data as
the function is guaranteed to NULL-terminate the buffer.

Workaround this problem and add a test for it.