Skip to content

Commit 4b87474

Browse files
committed
grep -An -Bm: fix invocation of external grep command
When building command line to invoke external grep, the arguments to -A/-B/-C options were placd in randarg[] buffer, but the code forgot that snprintf() does not count terminating NUL in its return value. This caused "git grep -A1 -B2" to invoke external grep with "-B21 -A1". Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 78e6947 commit 4b87474

File tree

1 file changed

+3
-3
lines changed

1 file changed

+3
-3
lines changed

builtin-grep.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -294,7 +294,7 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
294294
if (opt->pre_context) {
295295
push_arg("-B");
296296
len += snprintf(argptr, sizeof(randarg)-len,
297-
"%u", opt->pre_context);
297+
"%u", opt->pre_context) + 1;
298298
if (sizeof(randarg) <= len)
299299
die("maximum length of args exceeded");
300300
push_arg(argptr);
@@ -303,7 +303,7 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
303303
if (opt->post_context) {
304304
push_arg("-A");
305305
len += snprintf(argptr, sizeof(randarg)-len,
306-
"%u", opt->post_context);
306+
"%u", opt->post_context) + 1;
307307
if (sizeof(randarg) <= len)
308308
die("maximum length of args exceeded");
309309
push_arg(argptr);
@@ -313,7 +313,7 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
313313
else {
314314
push_arg("-C");
315315
len += snprintf(argptr, sizeof(randarg)-len,
316-
"%u", opt->post_context);
316+
"%u", opt->post_context) + 1;
317317
if (sizeof(randarg) <= len)
318318
die("maximum length of args exceeded");
319319
push_arg(argptr);

0 commit comments

Comments
 (0)