git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH] mailinfo: resolve -Wstring-plus-int warning

From
Jeff King <peff@peff.net>
Date
Sep 23, 2014, 06:04 UTC
Message-ID
<20140923060407.GA23861@peff.net>
In-Reply-To
<CAPig+cTAFaG5H8rmf1jrvFwr_OOH7u19JMKDUE12UddokUmfaQ@mail.gmail.com>
On Mon, Sep 22, 2014 at 05:10:08PM -0400, Eric Sunshine wrote:
Show 21 quoted lines
> On Mon, Sep 22, 2014 at 1:41 PM, Junio C Hamano <gitster@pobox.com> wrote:
> > Eric Sunshine <sunshine@sunshineco.com> writes:
> >
> >> 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?
> 
> Not being privy to Apple's decision making process, I can only guess
> that it is in response to their new Swift programming language which
> they are pushing heavily on iOS (and soon Mac OS X), in which '+' is
> the string concatenation operator. For projects written in Swift and
> incorporating legacy or portable components in C, C++, or Objective-C,
> the warning may help programmer's avoid the pitfall of thinking that
> '+' is also concatenation in the C-based languages.

That is my reading from the warning text, too, but I have to wonder: wouldn't that mean they should be warning about pointer + pointer, not pointer + int?

Also, wouldn't the same advice apply to adding to _any_ char pointer, not just a string literal?

I know you don't have answers to those questions, but the whole thing seems rather silly to me.

> > Can we have them fix their compiler instead?
> 
> If the above supposition is correct, then it's likely that Apple
> considers this a feature, not a bug which needs to be fixed.

I don't mind silencing this one warning (even though I find it a little ridiculous). I'm slightly concerned that more brain-damage may be coming our way, but we can deal with that if it ever does.

Like Junio, I prefer keeping strlen() rather than switching to sizeof, as it is less error-prone (no need for extra "-1" dance, and it won't silently do the wrong thing if the array is ever converted to a pointer).

-Peff
Previous: Eric SunshineNext: Junio C Hamano
Message 4 of 12 in “mailinfo: resolve -Wstring-plus-int warning”
  1. mailinfo: resolve -Wstring-plus-int warningEric Sunshine, Sep 21, 2014
  2. Junio C HamanoSep 22, 2014
  3. Eric SunshineSep 22, 2014
  4. Jeff KingSep 23, 2014
  5. Junio C HamanoSep 23, 2014
  6. Jeff KingSep 23, 2014
  7. Eric SunshineSep 23, 2014
  8. Eric SunshineSep 23, 2014
  9. Eric SunshineSep 23, 2014
  10. Jeff KingSep 23, 2014
  11. Junio C HamanoSep 22, 2014
  12. Eric SunshineSep 22, 2014

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.