Skip to content

Commit 995e525

Browse files
peffgitster
authored andcommitted
grep: stop modifying buffer in show_line()
When showing lines via grep (or looking for funcnames), we call show_line() on a multi-line buffer. It finds the end of line and marks it with a NUL. However, we don't need to do so, as the resulting line is only used along with its "eol" marker: - we pass both to next_match(), which takes care to look at only the bytes we specified - we pass the line to output_color() without its matching eol marker. However, we do use the "match" struct we got from next_match() to tell it how many bytes to look at (which can never exceed the string we passed it). So we can stop setting and restoring this NUL marker. That makes the code simpler, and will allow us to take a const buffer in a future patch. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cc8e26e commit 995e525

File tree

1 file changed

+0
-3
lines changed

1 file changed

+0
-3
lines changed

grep.c

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1239,7 +1239,6 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
12391239
if (opt->color || opt->only_matching) {
12401240
regmatch_t match;
12411241
enum grep_context ctx = GREP_CONTEXT_BODY;
1242-
int ch = *eol;
12431242
int eflags = 0;
12441243

12451244
if (opt->color) {
@@ -1254,7 +1253,6 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
12541253
else if (sign == '=')
12551254
line_color = opt->colors[GREP_COLOR_FUNCTION];
12561255
}
1257-
*eol = '\0';
12581256
while (next_match(opt, bol, eol, ctx, &match, eflags)) {
12591257
if (match.rm_so == match.rm_eo)
12601258
break;
@@ -1272,7 +1270,6 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol,
12721270
rest -= match.rm_eo;
12731271
eflags = REG_NOTBOL;
12741272
}
1275-
*eol = ch;
12761273
}
12771274
if (!opt->only_matching) {
12781275
output_color(opt, bol, rest, line_color);

0 commit comments

Comments
 (0)