From: Stefan Beller <sbeller@google.com>
To: Philip Oakley <philipoakley@iee.org>
Cc: Junio C Hamano <gitster@pobox.com>,
Stefan Naewe <stefan.naewe@gmail.com>,
"git@vger.kernel.org" <git@vger.kernel.org>,
Stefan Beller <stefanbeller@googlemail.com>
Subject: Re: [PATCH] revision.c: fix possible null pointer access
Date: Thu, 3 Dec 2015 14:17:01 -0800 [thread overview]
Message-ID: <CAGZ79kYRVDLooqTR2fRzoOVs2u2TeOubF9-wX9YVEVqHMOfT3Q@mail.gmail.com> (raw)
In-Reply-To: <46311B14CC814F54AC34764F2520947A@PhilipOakley>
On Thu, Dec 3, 2015 at 1:34 PM, Philip Oakley <philipoakley@iee.org> wrote:
> From: "Junio C Hamano" <gitster@pobox.com>
>>
>> Stefan Naewe <stefan.naewe@gmail.com> writes:
>>
>>> Two functions dereference a tree pointer before checking
>>
>>
>> Reading them a bit carefully, a reader would notice that they
>> actually do not dereference the pointer at all. It just computes
>> another pointer and that is done by adding the offset of object
>> member in the tree struct.
Well compiler people want their compiler to produce the best output,
meaning the compiled code goes fast.
So if you ask a compiler-writer, this may qualify enough for
being a dereference, because it looks like a dereference.
Assuming this is a dereference, you can further reason about
upcoming
if (pointer)
As the pointer was already dereferenced, it can be assumed not NULL.
(the pointer being NULL would be undefined behavior, in which the
compiler can do whatever it wants, i.e. that case can be ignored)
So with the strong assumption of the pointer being not NULL, you
can optimize away an
if (pointer)
as that is "always" false.
In case the pointer is NULL, we have had undefined behavior, so
the compiler is allowed to generate wrong code.
Which is why the if(pointer) is removed from the compiled binary,
as less instructions make the code go faster.
>
> But you can't do that computation (in the error case under consideration).
> Null can't be added to anything (as far as the implications of the standards
> go). These are horrid gotchas because they go against the grain of all that
> binary arithmetic and simplifications we learnt long ago.
>
> That said, the fact that we know it can't be null does save the day, until
> that is, the compiler [via some coding of an interpretation] decides that it
> could be null and thus undefined etc etc (which one would argue as poor
> logic, but standards have no truck with such arguments;-).
>
> There were some discussion on undefined behaviour way back (2013-08-08) when
> Stephan Beller looked at STACK's checking of the Git code, see for example
> http://article.gmane.org/gmane.comp.version-control.git/231945/
> "3 issues have been discovered using the STACK tool
> The paper regarding that tool can be found at
> https://pdos.csail.mit.edu/papers/stack:sosp13.pdf" (link updated)
Yeah that tool would detect such a bug. I can see
if I can get it to run frequently and post results somewhere.
IIRC it was quite a pain to get it working correctly on Git and
then reasoning for the resulting patch.
>
> All their source code is publicly available at
> http://css.csail.mit.edu/stack/
Thanks for pointing to that tool again. :)
next prev parent reply other threads:[~2015-12-03 22:17 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2015-12-03 19:32 [PATCH] revision.c: fix possible null pointer access Stefan Naewe
2015-12-03 20:06 ` Junio C Hamano
2015-12-03 21:15 ` Stefan Naewe
2015-12-03 21:34 ` Philip Oakley
2015-12-03 22:17 ` Stefan Beller [this message]
2015-12-04 15:39 ` Junio C Hamano
2015-12-04 23:32 ` Jeff King
2015-12-05 15:27 ` [PATCH v2] " Stefan Naewe
2015-12-07 20:31 ` Junio C Hamano
2015-12-07 21:54 ` Johannes Sixt
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=CAGZ79kYRVDLooqTR2fRzoOVs2u2TeOubF9-wX9YVEVqHMOfT3Q@mail.gmail.com \
--to=sbeller@google.com \
--cc=git@vger.kernel.org \
--cc=gitster@pobox.com \
--cc=philipoakley@iee.org \
--cc=stefan.naewe@gmail.com \
--cc=stefanbeller@googlemail.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).