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>
Junio C Hamano committed
Nov 17, 2007 at 21:18 UTC
4b87474bc9b620cf07b09130e33963cdaf34c603
1 file changed
+3
-3
builtin-grep.c
+3
-3
@@ -294,7 +294,7 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
294
if (opt->pre_context) {
295
push_arg("-B");
296
len += snprintf(argptr, sizeof(randarg)-len,
297
- "%u", opt->pre_context);
297
+ "%u", opt->pre_context) + 1;
298
if (sizeof(randarg) <= len)
299
die("maximum length of args exceeded");
300
push_arg(argptr);
@@ -303,7 +303,7 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
303
if (opt->post_context) {
304
push_arg("-A");
305
len += snprintf(argptr, sizeof(randarg)-len,
306
- "%u", opt->post_context);
306
+ "%u", opt->post_context) + 1;
307
if (sizeof(randarg) <= len)
308
die("maximum length of args exceeded");
309
push_arg(argptr);
@@ -313,7 +313,7 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
313
else {
314
push_arg("-C");
315
len += snprintf(argptr, sizeof(randarg)-len,
316
- "%u", opt->post_context);
316
+ "%u", opt->post_context) + 1;
317
if (sizeof(randarg) <= len)
318
die("maximum length of args exceeded");
319
push_arg(argptr);