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 <peff@peff.net> Signed-off-by: Junio C Hamano <gitster@pobox.com>
Jeff King committed
Sep 20, 2021 at 23:48 UTC
f84e79ff4bffbcd9de85adc270f5164a6b024d34
1 file changed
+1
-4
grep.c
+1
-4
@@ -1616,7 +1616,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
1616
bol = gs->buf;
1617
left = gs->size;
1618
while (left) {
1619
- char *eol, ch;
1619
+ char *eol;
1620
int hit;
1621
ssize_t cno;
1622
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
1637
&& look_ahead(opt, &left, &lno, &bol))
1638
break;
1639
eol = end_of_line(bol, &left);
1640
- ch = *eol;
1641
- *eol = 0;
1640
1641
if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
1642
ctx = GREP_CONTEXT_BODY;
1643
1644
hit = match_line(opt, bol, eol, &col, &icol, ctx, collect_hits);
1647
- *eol = ch;
1645
1646
if (collect_hits)
1647
goto next_line;