From: Junio C Hamano <gitster@pobox.com>
To: "René Scharfe" <l.s.r@web.de>
Cc: Stefan Beller <sbeller@google.com>, Jeff King <peff@peff.net>,
"git\@vger.kernel.org" <git@vger.kernel.org>
Subject: Re: Fwd: New Defects reported by Coverity Scan for git
Date: Thu, 20 Jul 2017 14:52:33 -0700 [thread overview]
Message-ID: <xmqq7ez2kc7y.fsf@gitster.mtv.corp.google.com> (raw)
In-Reply-To: <0110b370-a515-df44-8d46-9a72ac336ee7@web.de> ("René Scharfe"'s message of "Thu, 20 Jul 2017 22:32:56 +0200")
René Scharfe <l.s.r@web.de> writes:
> We could remove that NULL check -- it's effectively just a shortcut.
> But how would that improve safety? Well, if the array is unallocated
> (NULL) and _num is greater than zero we'd get a segfault without it,
> and thus would notice it. That check currently papers over such a
> hypothetical bug. Makes sense?
>
> -- >8 --
> Subject: [PATCH] pack-objects: remove unnecessary NULL check
>
> If done_pbase_paths is NULL then done_pbase_paths_num must be zero and
> done_pbase_path_pos() returns -1 without accessing the array, so the
> check is not necessary.
>
> If the invariant was violated then the check would make sure we keep
> on going and allocate the necessary amount of memory in the next
> ALLOC_GROW call. That sounds nice, but all array entries except for
> one would contain garbage data.
>
> If the invariant was violated without the check we'd get a segfault in
> done_pbase_path_pos(), i.e. an observable crash, alerting us of the
> presence of a bug.
>
> Currently there is no such bug: Only the functions check_pbase_path()
> and cleanup_preferred_base() change pointer and counter, and both make
> sure to keep them in sync. Get rid of the check anyway to allow us to
> see if later changes introduce such a defect, and to simplify the code.
>
> Detected by Coverity Scan.
>
> Signed-off-by: Rene Scharfe <l.s.r@web.de>
> ---
It's always amusing to see that a removal of conditional codepath
would result in better chance of finding possible invariant
breakers, as we often think that belt-and-suspenders safety would
require more conditionals and asserts ;-)
> builtin/pack-objects.c | 2 +-
> 1 file changed, 1 insertion(+), 1 deletion(-)
>
> diff --git a/builtin/pack-objects.c b/builtin/pack-objects.c
> index e730b415bf..c753e9237a 100644
> --- a/builtin/pack-objects.c
> +++ b/builtin/pack-objects.c
> @@ -1289,7 +1289,7 @@ static int done_pbase_path_pos(unsigned hash)
>
> static int check_pbase_path(unsigned hash)
> {
> - int pos = (!done_pbase_paths) ? -1 : done_pbase_path_pos(hash);
> + int pos = done_pbase_path_pos(hash);
> if (0 <= pos)
> return 1;
> pos = -pos - 1;
next prev parent reply other threads:[~2017-07-20 21:52 UTC|newest]
Thread overview: 20+ messages / expand[flat|nested] mbox.gz Atom feed top
[not found] <596ddaa620821_77f83e7330107c4@ss1435.mail>
2017-07-18 16:59 ` Fwd: New Defects reported by Coverity Scan for git Stefan Beller
2017-07-18 17:23 ` Junio C Hamano
2017-07-18 17:41 ` Stefan Beller
2017-07-18 17:55 ` Junio C Hamano
2017-07-18 18:00 ` Brandon Williams
2017-07-18 18:06 ` Stefan Beller
2017-07-20 20:32 ` René Scharfe
2017-07-20 21:52 ` Junio C Hamano [this message]
2018-03-26 23:39 Stefan Beller
2018-03-27 10:38 ` Jeff Hostetler
[not found] <580893d5a4736_4ed37b53181837@ss1435.mail>
2016-10-20 17:05 ` Stefan Beller
2016-10-20 17:50 ` Junio C Hamano
2016-10-20 17:58 ` Stefan Beller
2016-10-20 18:05 ` Junio C Hamano
2016-10-20 18:13 ` Stefan Beller
2016-10-20 21:42 ` Jeff King
2016-10-20 22:07 ` Junio C Hamano
2016-10-20 21:40 ` Jeff King
[not found] <55bb53d17f78c_2d71521318537c@scan.mail>
2015-07-31 11:24 ` Duy Nguyen
[not found] <558151df465a5_4fafe3b3182568a@scan.mail>
2015-06-17 13:54 ` Duy Nguyen
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=xmqq7ez2kc7y.fsf@gitster.mtv.corp.google.com \
--to=gitster@pobox.com \
--cc=git@vger.kernel.org \
--cc=l.s.r@web.de \
--cc=peff@peff.net \
--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 an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.