git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] mailinfo: resolve -Wstring-plus-int warning
@ 2014-09-21  9:13 Eric Sunshine
  2014-09-22 17:41 ` Junio C Hamano
  2014-09-22 20:50 ` Junio C Hamano
  0 siblings, 2 replies; 12+ messages in thread
From: Eric Sunshine @ 2014-09-21  9:13 UTC (permalink / raw)
  To: git; +Cc: Eric Sunshine, Jeff King

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) ...
                           ~~~~~~~^~~~~~~~~~~~~

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,
-- 
2.1.1.391.g7a54a76.dirty

^ permalink raw reply related	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2014-09-23  8:13 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2014-09-21  9:13 [PATCH] mailinfo: resolve -Wstring-plus-int warning Eric Sunshine
2014-09-22 17:41 ` Junio C Hamano
2014-09-22 21:10   ` Eric Sunshine
2014-09-23  6:04     ` Jeff King
2014-09-23  6:26       ` Junio C Hamano
2014-09-23  7:51         ` Jeff King
2014-09-23  8:05           ` Eric Sunshine
2014-09-23  7:58         ` Eric Sunshine
2014-09-23  7:52       ` Eric Sunshine
2014-09-23  8:12         ` Jeff King
2014-09-22 20:50 ` Junio C Hamano
2014-09-22 21:50   ` Eric Sunshine

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).