Skip to content

Commit

Permalink
grep -An -Bm: fix invocation of external grep command
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Junio C Hamano committed Nov 18, 2007
1 parent 78e6947 commit 4b87474
Showing 1 changed file with 3 additions and 3 deletions.
6 changes: 3 additions & 3 deletions builtin-grep.c
Original file line number Diff line number Diff line change
Expand Up @@ -294,7 +294,7 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
if (opt->pre_context) {
push_arg("-B");
len += snprintf(argptr, sizeof(randarg)-len,
"%u", opt->pre_context);
"%u", opt->pre_context) + 1;
if (sizeof(randarg) <= len)
die("maximum length of args exceeded");
push_arg(argptr);
Expand All @@ -303,7 +303,7 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
if (opt->post_context) {
push_arg("-A");
len += snprintf(argptr, sizeof(randarg)-len,
"%u", opt->post_context);
"%u", opt->post_context) + 1;
if (sizeof(randarg) <= len)
die("maximum length of args exceeded");
push_arg(argptr);
Expand All @@ -313,7 +313,7 @@ static int external_grep(struct grep_opt *opt, const char **paths, int cached)
else {
push_arg("-C");
len += snprintf(argptr, sizeof(randarg)-len,
"%u", opt->post_context);
"%u", opt->post_context) + 1;
if (sizeof(randarg) <= len)
die("maximum length of args exceeded");
push_arg(argptr);
Expand Down

0 comments on commit 4b87474

Please sign in to comment.