Re: [PATCH] mailinfo: resolve -Wstring-plus-int warning
- From
Junio C Hamano <gitster@pobox.com>
- Date
- Sep 22, 2014, 17:41 UTC
- Message-ID
- <xmqqk34vlfhz.fsf@gitster.dls.corp.google.com>
- In-Reply-To
- <1411290838-45622-1-git-send-email-sunshine@sunshineco.com>
Eric Sunshine <sunshine@sunshineco.com> writes:
Show 8 quoted lines
> The just-released Apple Xcode 6.0.1 has -Wstring-plus-int enabled by > default which complains about pointer arithmetic applied to a string > literal: > > builtin/mailinfo.c:303:24: warning: > adding 'long' to a string does not append to the string > return !memcmp(SAMPLE + (cp - line), cp, strlen(SAMPLE) ... > ~~~~~~~^~~~~~~~~~~~~
And why is that a warning-worthy violation? Can we have them fix their compiler instead?
Show 45 quoted lines
>
> Resolve this issue.
>
> Signed-off-by: Eric Sunshine <sunshine@sunshineco.com>
> ---
>
> This is atop 2da1f366 (mailinfo: make ">From" in-body header check more
> robust; 2014-09-13) in 'next'.
>
> In addition to the above diagnostic, the Apple compiler also helpfully
> recommends &SAMPLE[cp - line] as a replacement to avoid the warning,
> however, the solution in this patch allows us drop a couple strlen()s in
> favor of sizeof()s.
>
> builtin/mailinfo.c | 7 ++++---
> 1 file changed, 4 insertions(+), 3 deletions(-)
>
> diff --git a/builtin/mailinfo.c b/builtin/mailinfo.c
> index 2632fb0..b6b1c19 100644
> --- a/builtin/mailinfo.c
> +++ b/builtin/mailinfo.c
> @@ -288,19 +288,20 @@ static inline int cmp_header(const struct strbuf *line, const char *hdr)
> line->buf[len] == ':' && isspace(line->buf[len + 1]);
> }
>
> -#define SAMPLE "From e6807f3efca28b30decfecb1732a56c7db1137ee Mon Sep 17 00:00:00 2001\n"
> static int is_format_patch_separator(const char *line, int len)
> {
> + static const char SAMPLE[] =
> + "From e6807f3efca28b30decfecb1732a56c7db1137ee Mon Sep 17 00:00:00 2001\n";
> const char *cp;
>
> - if (len != strlen(SAMPLE))
> + if (len != sizeof(SAMPLE) - 1)
> return 0;
> if (!skip_prefix(line, "From ", &cp))
> return 0;
> if (strspn(cp, "0123456789abcdef") != 40)
> return 0;
> cp += 40;
> - return !memcmp(SAMPLE + (cp - line), cp, strlen(SAMPLE) - (cp - line));
> + return !memcmp(SAMPLE + (cp - line), cp, sizeof(SAMPLE) - 1 - (cp - line));
> }
>
> static int check_header(const struct strbuf *line,