{"thread":{"id":"10910","subject":"[PATCH] grep -An -Bm: fix invocation of external grep command","startedAt":"2007-11-18T05:18:14Z","lastAt":"2007-11-18T05:18:14Z","messageCount":1,"participants":["Junio C Hamano"],"isPatch":true,"patchVersion":1,"patchTotal":null},"messages":[{"id":"60203","messageId":"7vk5ogxhbd.fsf@gitster.siamese.dyndns.org","threadId":"10910","inReplyTo":null,"subject":"[PATCH] grep -An -Bm: fix invocation of external grep command","fromName":"Junio C Hamano","fromEmail":"gitster@pobox.com","sentAt":"2007-11-18T05:18:14Z","receivedAt":"2007-11-18T05:18:14Z","isPatch":true,"sender":{"key":"gitster@pobox.com","avatar":"https://avatars.githubusercontent.com/u/54884?v=4"},"body":"When building command line to invoke external grep, the\narguments to -A/-B/-C options were placd in randarg[] buffer,\nbut the code forgot that snprintf() does not count terminating\nNUL in its return value.  This caused \"git grep -A1 -B2\" to\ninvoke external grep with \"-B21 -A1\".\n\nSigned-off-by: Junio C Hamano <gitster@pobox.com>\n---\n\n * This bug was present since mid May, 2006.\n\n builtin-grep.c |    6 +++---\n 1 files changed, 3 insertions(+), 3 deletions(-)\n\ndiff --git a/builtin-grep.c b/builtin-grep.c\nindex 185876b..bbf747f 100644\n--- a/builtin-grep.c\n+++ b/builtin-grep.c\n@@ -294,7 +294,7 @@ static int external_grep(struct grep_opt *opt, const char\n \t\t\tif (opt->pre_context) {\n \t\t\t\tpush_arg(\"-B\");\n \t\t\t\tlen += snprintf(argptr, sizeof(randarg)-len,\n-\t\t\t\t\t\t\"%u\", opt->pre_context);\n+\t\t\t\t\t\t\"%u\", opt->pre_context) + 1;\n \t\t\t\tif (sizeof(randarg) <= len)\n \t\t\t\t\tdie(\"maximum length of args exceeded\");\n \t\t\t\tpush_arg(argptr);\n@@ -303,7 +303,7 @@ static int external_grep(struct grep_opt *opt, const char\n \t\t\tif (opt->post_context) {\n \t\t\t\tpush_arg(\"-A\");\n \t\t\t\tlen += snprintf(argptr, sizeof(randarg)-len,\n-\t\t\t\t\t\t\"%u\", opt->post_context);\n+\t\t\t\t\t\t\"%u\", opt->post_context) + 1;\n \t\t\t\tif (sizeof(randarg) <= len)\n \t\t\t\t\tdie(\"maximum length of args exceeded\");\n \t\t\t\tpush_arg(argptr);\n@@ -313,7 +313,7 @@ static int external_grep(struct grep_opt *opt, const char\n \t\telse {\n \t\t\tpush_arg(\"-C\");\n \t\t\tlen += snprintf(argptr, sizeof(randarg)-len,\n-\t\t\t\t\t\"%u\", opt->post_context);\n+\t\t\t\t\t\"%u\", opt->post_context) + 1;\n \t\t\tif (sizeof(randarg) <= len)\n \t\t\t\tdie(\"maximum length of args exceeded\");\n \t\t\tpush_arg(argptr);\n"}]}