git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jeff King <peff@peff.net>
To: Johannes Schindelin <johannes.schindelin@gmx.de>
Cc: Stefan Beller <sbeller@google.com>,
	Michael Blume <blume.mike@gmail.com>,
	peter@lekensteyn.nl, eungjun.yi@navercorp.com,
	Git List <git@vger.kernel.org>
Subject: Re: Git compile warnings (under mac/clang)
Date: Fri, 23 Jan 2015 07:23:18 -0500	[thread overview]
Message-ID: <20150123122317.GA12517@peff.net> (raw)
In-Reply-To: <315bf23981813799d16fdd9b533444f3@www.dscho.org>

On Fri, Jan 23, 2015 at 12:48:29PM +0100, Johannes Schindelin wrote:

> This is what I have currently in the way of attempting to "fix" it (I
> still believe that Clang is wrong to make this a warning, and causes
> more trouble than it solves):

I agree. It is something we as the programmers cannot possibly know (the
compiler is free to decide which type however it likes) and its decision
does not impact the correctness of the code (the check is either useful
or tautological, and we cannot know which, so we are being warned about
being too careful!).

I guess you could argue that the standard defines enum-numbering to
start at 0, and increment by 1.  Therefore we should know that no valid
enum value is less than 0.  IOW, "msg_id < 0" being true must be the
result of a bug somewhere else in the program, where we assigned a value
outside of the enum range to the enum.

>     Pointed out by Michael Blume. Jeff King provided the pointer to a commit
>     fixing the same issue elsewhere in the Git source code.

It may be useful to reference the exact commit (3ce3ffb8) to help people
digging in the history (e.g., if we decide there is a better way to shut
up this warning and we need to find all the places to undo the
brain-damage).

> -	for (i = 0; i < FSCK_MSG_MAX; i++) {
> +	for (i = FSCK_MSG_MIN + 1; i < FSCK_MSG_MAX; i++) {

Ugh. It is really a shame how covering up this warning requires
polluting so many places. I don't think we have a better way, though,
aside from telling people to use -Wno-tautological-compare (and I can
believe that it _is_ a useful warning in some other circumstances, so it
seems a shame to lose it).

Unless we are willing to drop the ">= 0" check completely. I think it is
valid to do so regardless of the compiler's representation decision due
to the numbering rules I mentioned above. It kind-of serves as a
cross-check that we haven't cast some random int into the enum, but I
think we would do better to find those callsites (since they are not
guaranteed to work, anyway; in addition to signedness, it might choose a
much smaller representation).

I do not see either side of the bounds check here:

> +	if (options->msg_severity &&
> +			msg_id > FSCK_MSG_MIN && msg_id < FSCK_MSG_MAX)

as really doing anything. Any code which triggers it must already cause
undefined behavior, I think (with the exception of "msg_id == FSCK_MSG_MAX",
but presumably that is something we never expect to happen, either).

-Peff

  reply	other threads:[~2015-01-23 12:23 UTC|newest]

Thread overview: 15+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-01-22 19:43 Git compile warnings (under mac/clang) Michael Blume
2015-01-22 19:59 ` Stefan Beller
2015-01-22 21:19   ` Peter Wu
2015-01-22 21:20   ` Johannes Schindelin
2015-01-22 22:01     ` Jeff King
2015-01-23 11:48       ` Johannes Schindelin
2015-01-23 12:23         ` Jeff King [this message]
2015-01-23 12:38           ` Johannes Schindelin
2015-01-23 13:30             ` Jeff King
2015-01-23 18:07               ` Junio C Hamano
2015-01-23 18:37                 ` Jeff King
2015-01-23 18:46                   ` Johannes Schindelin
2015-01-23 18:55                     ` Jeff King
2015-01-23 19:20                       ` Johannes Schindelin
2015-01-23 18:48                   ` Junio C Hamano

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20150123122317.GA12517@peff.net \
    --to=peff@peff.net \
    --cc=blume.mike@gmail.com \
    --cc=eungjun.yi@navercorp.com \
    --cc=git@vger.kernel.org \
    --cc=johannes.schindelin@gmx.de \
    --cc=peter@lekensteyn.nl \
    --cc=sbeller@google.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).