Skip to content

Commit f84e79f

Browse files
peffgitster
authored andcommitted
grep: stop modifying buffer in grep_source_1()
We find the end of each matching line of a buffer, and then temporarily write a NUL to turn it into a regular C string. But we don't need to do so, because the only thing we do in the interim is pass the line and its length (via an "eol" pointer) to match_line(). And that function should only look at the bytes we passed it, whether it has a terminating NUL or not. We can drop this temporary write in order to simplify the code and make it easier to use const buffers in more of grep.c. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 995e525 commit f84e79f

File tree

1 file changed

+1
-4
lines changed

1 file changed

+1
-4
lines changed

grep.c

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1616,7 +1616,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
16161616
bol = gs->buf;
16171617
left = gs->size;
16181618
while (left) {
1619-
char *eol, ch;
1619+
char *eol;
16201620
int hit;
16211621
ssize_t cno;
16221622
ssize_t col = -1, icol = -1;
@@ -1637,14 +1637,11 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
16371637
&& look_ahead(opt, &left, &lno, &bol))
16381638
break;
16391639
eol = end_of_line(bol, &left);
1640-
ch = *eol;
1641-
*eol = 0;
16421640

16431641
if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
16441642
ctx = GREP_CONTEXT_BODY;
16451643

16461644
hit = match_line(opt, bol, eol, &col, &icol, ctx, collect_hits);
1647-
*eol = ch;
16481645

16491646
if (collect_hits)
16501647
goto next_line;

0 commit comments

Comments
 (0)