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); Jakub