[Bug c++/70403] A null pointer check removed with -O2 even with -fno-delete-null-pointer-checks

redi at gcc dot gnu.org gcc-bugzilla@gcc.gnu.org
Fri Apr 1 09:02:00 GMT 2016


https://gcc.gnu.org/bugzilla/show_bug.cgi?id=70403

--- Comment #11 from Jonathan Wakely <redi at gcc dot gnu.org> ---
(In reply to Richard Biener from comment #10)
>     McCpId* pCpId = static_cast<McCpId*>(&newCpId);
> 
> and it's further use.  You can't use an object of McId via a pointer to
> McCpId but you call
> 
>     pCpId->toString (cpIdBuf, sizeof (cpIdBuf));
> 
> on it.

You don't even need to use it through the derived pointer, simply performing
the static_cast is undefined behaviour:

  struct base { };
  struct derived : base { };
  base b;
  static_cast<derived*>(&b);  // undefined

Compiling with -fsanitize=undefined would have found that bug.


More information about the Gcc-bugs mailing list