summaryrefslogtreecommitdiff
path: root/grep.c
diff options
context:
space:
mode:
authorJeff King <peff@peff.net>2021-09-21 03:48:44 (GMT)
committerJunio C Hamano <gitster@pobox.com>2021-09-22 18:59:50 (GMT)
commitf84e79ff4bffbcd9de85adc270f5164a6b024d34 (patch)
treee397f31ee0e963c6d653af58689664de99ff1abc /grep.c
parent995e525b1729ada354e443f16e1c0fad59df25a8 (diff)
downloadgit-f84e79ff4bffbcd9de85adc270f5164a6b024d34.zip
git-f84e79ff4bffbcd9de85adc270f5164a6b024d34.tar.gz
git-f84e79ff4bffbcd9de85adc270f5164a6b024d34.tar.bz2
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>
Diffstat (limited to 'grep.c')
-rw-r--r--grep.c5
1 files changed, 1 insertions, 4 deletions
diff --git a/grep.c b/grep.c
index 70af01d..32c4641 100644
--- a/grep.c
+++ b/grep.c
@@ -1616,7 +1616,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
bol = gs->buf;
left = gs->size;
while (left) {
- char *eol, ch;
+ char *eol;
int hit;
ssize_t cno;
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
&& look_ahead(opt, &left, &lno, &bol))
break;
eol = end_of_line(bol, &left);
- ch = *eol;
- *eol = 0;
if ((ctx == GREP_CONTEXT_HEAD) && (eol == bol))
ctx = GREP_CONTEXT_BODY;
hit = match_line(opt, bol, eol, &col, &icol, ctx, collect_hits);
- *eol = ch;
if (collect_hits)
goto next_line;