From 4b87474bc9b620cf07b09130e33963cdaf34c603 Mon Sep 17 00:00:00 2001 From: Junio C Hamano Date: Sat, 17 Nov 2007 21:18:14 -0800 Subject: [PATCH] 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 --- builtin-grep.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/builtin-grep.c b/builtin-grep.c index 185876b0a..bbf747fc7 100644 --- a/builtin-grep.c +++ b/builtin-grep.c @@ -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); @@ -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); @@ -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); -- 2.30.2