This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: GCC 4.9.2 -O3 gives a seg fault / GCC 4.8.2 -O3 works
- From: Paul Smith <paul at mad-scientist dot net>
- To: Jakub Jelinek <jakub at redhat dot com>
- Cc: gcc at gcc dot gnu dot org
- Date: Tue, 06 Jan 2015 08:50:58 -0500
- Subject: Re: GCC 4.9.2 -O3 gives a seg fault / GCC 4.8.2 -O3 works
- Authentication-results: sourceware.org; auth=none
- References: <1420532328 dot 7095 dot 263 dot camel at homebase> <20150106084350 dot GE1667 at tucnak dot redhat dot com>
- Reply-to: paul at mad-scientist dot net
On Tue, 2015-01-06 at 09:43 +0100, Jakub Jelinek wrote:
> On Tue, Jan 06, 2015 at 03:18:48AM -0500, Paul Smith wrote:
> > Hi all. It's possible my code is doing something illegal, but it's also
> > possible I've found a problem with -O3 optimization in GCC 4.9.2. I've
> > built this same code with GCC 4.8.2 -O3 on GNU/Linux and it works fine.
> > It also works with GCC 4.9.2 with lower -O (-O2 for example).
>
> Your testcase is invalid.
> GCC trunk -fsanitize=undefined (in particular -fsanitize=nonnull-attribute)
> diagnoses it:
> /tmp/mystring.cpp:103:26: runtime error: null pointer passed as argument 2, which is declared to never be null
> LD_PRELOAD=libmemstomp.so detects it too.
>
> Calling memcpy (p, NULL, 0); is invalid according to C and C++
> standards, you need to guard it, e.g. with if (data) memcpy (p, data, len1);
> or if (len1) memcpy (p, data, len1);
Ah interesting. You're right, this is definitely not correct. But
since len1 is 0 in this case, no implementation of memcpy() actually
tried to dereference the data pointer and so there was no failure (we
build and test with clang on OSX and MSVC on Windows, and run with
valgrind and ASAN (clang)).
I'll have to look at other possible failure situations.