Skip to content

Commit ae747b8

Browse files
peffGit for Windows Build Agent
authored and
Git for Windows Build Agent
committed
grep: prevent ^$ false match at end of file
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]>
1 parent 02808b8 commit ae747b8

File tree

1 file changed

+2
-0
lines changed

1 file changed

+2
-0
lines changed

grep.c

+2
Original file line numberDiff line numberDiff line change
@@ -1646,6 +1646,8 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
16461646

16471647
bol = gs->buf;
16481648
left = gs->size;
1649+
if (left && gs->buf[left-1] == '\n')
1650+
left--;
16491651
while (left) {
16501652
const char *eol;
16511653
int hit;

0 commit comments

Comments
 (0)