On OS X the printf specifier "%.0s" outputs the entire string instead of 0 characters as POSIX states.
In addition, for * width or precision printf expects an integer argument. On systems were regoff_t is 64-bit, unexpected results can occur.
To fix these, use if statements to catch 0 precisions and casts to convert regoff_t to int.
Signed-off-by: Brian Gernhardt <benji@silverinsanity.com> --- grep.c | 16 ++++++++++------ 1 files changed, 10 insertions(+), 6 deletions(-)
Show changes to grep.c +10 −6
diff --git a/grep.c b/grep.c index cace1c8..ec68200 100644 --- a/grep.c +++ b/grep.c @@ -489,18 +489,22 @@ static void show_line(struct grep_opt *opt, char *bol, char *eol, *eol = '\0'; while (next_match(opt, bol, eol, ctx, &match, eflags)) { - printf("%.*s%s%.*s%s", - match.rm_so, bol, - opt->color_match, - match.rm_eo - match.rm_so, bol + match.rm_so, - GIT_COLOR_RESET); + if( match.rm_so > 0 ) + printf( "%.*s", (int) match.rm_so, bol ); + if( match.rm_eo > match.rm_so ) + printf("%s%.*s%s", + opt->color_match, + (int) (match.rm_eo - match.rm_so), bol + match.rm_so, + GIT_COLOR_RESET); bol += match.rm_eo; rest -= match.rm_eo; eflags = REG_NOTBOL; } *eol = ch; } - printf("%.*s\n", rest, bol); + if( rest > 0 ) + printf("%.*s", rest, bol); + printf("\n"); } static int grep_buffer_1(struct grep_opt *opt, const char *name,
-- 1.6.2.222.g01cbd