Skip to content

Commit

Permalink
grep: prevent ^$ false match at end of file
Browse files Browse the repository at this point in the history
In some implementations, `regexec_buf()` assumes that it is fed lines;
Without `REG_NOTEOL` it thinks the end of the buffer is the end of a
line. Which makes sense, but trips up this case because we are not
feeding lines, but rather a whole buffer. So the final newline is not
the start of an empty line, but the true end of the buffer.

This causes an interesting bug:

  $ echo content >file.txt
  $ git grep --no-index -n '^$' file.txt
  file.txt:2:

This bug is fixed by making the end of the buffer consistently the end
of the final line.

The patch was applied from
https://lore.kernel.org/git/[email protected]/

Reported-by: Olly Betts <[email protected]>
Signed-off-by: Johannes Schindelin <[email protected]>
  • Loading branch information
peff authored and dscho committed Feb 6, 2025
1 parent 299435c commit a2e7060
Showing 1 changed file with 2 additions and 0 deletions.
2 changes: 2 additions & 0 deletions grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -1646,6 +1646,8 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle

bol = gs->buf;
left = gs->size;
if (left && gs->buf[left-1] == '\n')
left--;
while (left) {
const char *eol;
int hit;
Expand Down

0 comments on commit a2e7060

Please sign in to comment.