util: buf_getline: validate index of the next byte before reading it
util: buf_getline: validate index of the next byte before reading it

buf_getline dereferences cur[i] before checking that i is a valid index
in the buffer. Under the right circumstances, this can cause
a segmentation fault.

The right circumstances are:
  * multiline search disabled (the only way buf_getline is called)
  * file size is an exact multiple of the page size (4096 bytes)
  * end of file has no trailing newline
  * searching a directory (not just a single file on the command line)
  * using several worker threads

(I don't think the last two points are directly related to this bug, but
I couldn't reproduce it otherwise)

Address Sanitizer catches this consistently as a heap overflow if run
when mmap is disabled.

Swap the order of the for loop's condition checks so that i is validated
before dereferencing cur[i].

Signed-off-by: Allen Wild <c0235bafb4d09a895174208780decb59dc33d680@gmail.com>

sebgod/the_silver_searcher,sebgod/the_silver_searcher,JFLarvoire/the_silver_searcher,JFLarvoire/the_silver_searcher,sebgod/the_silver_searcher,lizh06/the_silver_searcher,JFLarvoire/the_silver_searcher,lizh06/the_silver_searcher,JFLarvoire/the_silver_searcher,JFLarvoire/the_silver_searcher,lizh06/the_silver_searcher,sebgod/the_silver_searcher,ggreer/the_silver_searcher,ggreer/the_silver_searcher,JFLarvoire/the_silver_searcher,ggreer/the_silver_searcher,ggreer/the_silver_searcher,JFLarvoire/the_silver_searcher,lizh06/the_silver_searcher