Fix bug discovered by valgrind.

When trying to merge lines, we should not touch lines that are invalid,
as we don't know how long they might be.