git@vger.kernel.org mailing list mirror (one of many)
 help / color / mirror / code / Atom feed
From: Jeff King <peff@peff.net>
To: "René Scharfe" <l.s.r@web.de>
Cc: Stephan Beyer <s-beyer@gmx.net>, Paul Tan <pyokagan@gmail.com>,
	"brian m. carlson" <sandals@crustytoothpaste.net>,
	Johannes Schindelin <Johannes.Schindelin@gmx.de>,
	git@vger.kernel.org
Subject: Re: [PATCH] Fix maybe-uninitialized warnings found by gcc 9 -flto
Date: Thu, 5 Sep 2019 13:53:05 -0400	[thread overview]
Message-ID: <20190905175304.GA23663@sigill.intra.peff.net> (raw)
In-Reply-To: <6c6c4e71-d9e5-1ad4-74db-12c323da42f7@web.de>

On Thu, Sep 05, 2019 at 07:08:49PM +0200, René Scharfe wrote:

> Anyway, my points are that simply initializing might not always be the
> best fix, and that more context would help reviewers of such a patch,
> but only if functions are reasonably short and it's not necessary to
> follow the rabbit into a call chain hole.

I started looking at these, too, and came to the same conclusion.  Some
of these are pointing to real bugs, and just silencing the warnings
misses the opportunity to find them.

I'll comment on the ones I did look at.

> Further context:
> 
> 	ident_line = find_commit_header(buffer, "author", &ident_len);
> 
> 	if (split_ident_line(&id, ident_line, ident_len) < 0)
> 		die(_("invalid ident line: %.*s"), (int)ident_len, ident_line);
> 
> find_commit_header() can return NULL.  split_ident_line() won't handle
> that well.  So I think what's missing here is a NULL check.  If the
> compiler is smart enough then that should silence the initialization
> warning.

Yeah, this one is a segfault waiting to happen, I think, if the author
line is missing.

> > diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
> > index 76ce906946..d0c03b0e9b 100644
> > --- a/builtin/pack-objects.c
> > +++ b/builtin/pack-objects.c
> > @@ -1171,7 +1171,7 @@ static int add_object_entry(const struct object_id *oid, enum object_type type,
> >  {
> >  	struct packed_git *found_pack = NULL;
> >  	off_t found_offset = 0;
> > -	uint32_t index_pos;
> > +	uint32_t index_pos = 0;
> [...]
> So if the packing list is empty then it returns NULL without setting
> index_pos.  Hmm.  It does set it in all other cases, no matter if oid is
> found or not.  Is it really a good idea to make that exception?  I
> suspect always setting index_pos here would silence the compiler as well
> and fix the issue closer to its root.

Yeah, this is a weird one. That index_pos is actually a position in the
current hash table. And in the first object we see in pack-objects'
input, we definitely _do_ end up with a nonsense index_pos, which then
gets passed to packlist_alloc().

But since we also need to grow the hash table during that allocation, we
don't use index_pos at all. So setting index_pos to something known like
"0" is kind of weird, in that we don't have a hash position at all (the
table doesn't exist!). But it's probably the least bad thing. If we do
that, it should happen in packlist_find().

I have to agree this whole "passing around index_pos" thing looks
complicated. It seems like we're just saving ourselves one hash lookup
on insertion (and it's not even an expensive hash, since we're reusing
the oid).

I suspect the whole thing would also be a lot simpler using one of our
existing hash implementations.

> Didn't check the other cases.

The only other one I looked at was:

>> int cmd__read_cache(int argc, const char **argv)
>> {
>>-       int i, cnt = 1, namelen;
>>+       int i, cnt = 1, namelen = 0;

I actually saw this one the other day, because it triggered for me when
compiling with SANITIZE=address. AFAICT it's a false positive. "name" is
always NULL unless skip_prefix() returns true, in which case we always
set "namelen". And we only look at "namelen" if "name" is non-NULL.

This one doesn't even require LTO, because skip_prefix() is an inline
function. I'm not sure why the compiler gets confused here. I don't mind
initializing namelen to 0 to silence it, though (we already set name to
NULL, so this would just match).

-Peff

  reply	other threads:[~2019-09-05 17:53 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-09-05  8:24 [PATCH] Fix maybe-uninitialized warnings found by gcc 9 -flto Stephan Beyer
2019-09-05 17:08 ` René Scharfe
2019-09-05 17:53   ` Jeff King [this message]
2019-09-05 19:09     ` René Scharfe
2019-09-05 19:25       ` Junio C Hamano
2019-09-05 19:39         ` René Scharfe
2019-09-05 19:50           ` Junio C Hamano
2019-09-05 17:41 ` Junio C Hamano
2019-09-05 17:56 ` Junio C Hamano
2019-09-05 18:03   ` Jeff King
2019-09-05 18:22     ` Junio C Hamano
2019-09-05 22:48 ` Jeff King
2019-09-05 22:50   ` [PATCH 1/6] git-am: handle missing "author" when parsing commit Jeff King
2019-09-05 22:52   ` [PATCH 2/6] pack-objects: use object_id in packlist_alloc() Jeff King
2019-09-05 22:52   ` [PATCH 3/6] bulk-checkin: zero-initialize hashfile_checkpoint Jeff King
2019-09-05 22:53   ` [PATCH 4/6] diff-delta: set size out-parameter to 0 for NULL delta Jeff King
2019-09-05 22:53   ` [PATCH] Fix maybe-uninitialized warnings found by gcc 9 -flto Stephan Beyer
2019-09-05 22:58     ` Jeff King
2019-09-05 23:10       ` Stephan Beyer
2019-09-06  1:24         ` Jeff King
2019-09-06  1:36           ` [PATCH v2 6/6] pack-objects: drop packlist index_pos optimization Jeff King
2019-09-05 22:54   ` [PATCH 5/6] test-read-cache: drop namelen variable Jeff King
2019-09-05 22:56   ` [PATCH 6/6] pack-objects: drop packlist index_pos optimization Jeff King

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

  List information: http://vger.kernel.org/majordomo-info.html

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

  git send-email \
    --in-reply-to=20190905175304.GA23663@sigill.intra.peff.net \
    --to=peff@peff.net \
    --cc=Johannes.Schindelin@gmx.de \
    --cc=git@vger.kernel.org \
    --cc=l.s.r@web.de \
    --cc=pyokagan@gmail.com \
    --cc=s-beyer@gmx.net \
    --cc=sandals@crustytoothpaste.net \
    /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.
Code repositories for project(s) associated with this public inbox

	https://80x24.org/mirrors/git.git

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 read-only IMAP folder(s) and NNTP newsgroup(s).