Skip to content

gcc8: static_cast NULL instead of reinterpret_cast

username-removed-195241 requested to merge illwieckz/netradiant:gcc8 into master

That's a bug I first got trying to compile GtkRadiant on FreeBSD using GCC8 (see GtkRadiant#564), but it happens with CLANG 4 too while trying to compile NetRadiant on FreeBSD, probably due to some higher c++ version enforced build. This is the error I get:

g++ -o build/release/shobjs/libs/splines/util_str.os -c -pipe -Wall -fmessage-length=0 -fvisibility=hidden -I/usr/local/include/libxml2 -I/usr/include -fpermissive -fvisibility-inlines-hidden -O2 -fno-strict-aliasing -pipe -Wall -fmessage-length=0 -fvisibility=hidden -I/usr/local/include/libxml2 -I/usr/include -fPIC -DQ_NO_STLPORT -I/usr/local/include/glib-2.0 -I/usr/local/lib/glib-2.0/include -I/usr/local/include -Ibuild/release/shobjs/include -Iinclude -Ibuild/release/shobjs/libs -Ilibs libs/splines/util_str.cpp
libs/splines/util_str.cpp: In function 'void TestStringClass()':
libs/splines/util_str.cpp:483:51: error: invalid cast from type 'std::nullptr_t' to type 'const char*'
  idStr e( reinterpret_cast<const char *>( NULL ) );
                                                   ^
libs/splines/util_str.cpp:502:50: error: invalid cast from type 'std::nullptr_t' to type 'const char*'
  a = c + reinterpret_cast<const char *>( NULL );
                                                  ^
libs/splines/util_str.cpp:505:46: error: invalid cast from type 'std::nullptr_t' to type 'const char*'
  a = reinterpret_cast<const char *>( NULL ) + d;
                                              ^
libs/splines/util_str.cpp:509:47: error: invalid cast from type 'std::nullptr_t' to type 'const char*'
  a += reinterpret_cast<const char *>( NULL );
                                               ^
scons: *** [build/release/shobjs/libs/splines/util_str.os] Error 1
scons: building terminated because of errors.

I read there about reinterpret_cast that:

A value of any integral or enumeration type can be converted to a pointer type. A pointer converted to an integer of sufficient size and back to the same pointer type is guaranteed to have its original value, otherwise the resulting pointer cannot be dereferenced safely (the round-trip conversion in the opposite direction is not guaranteed; the same pointer may have multiple integer representations) The null pointer constant NULL or integer zero is not guaranteed to yield the null pointer value of the target type; static_cast or implicit conversion should be used for this purpose.

So I substituted all the reinterpret_cast on NULL on this file with static_cast, and it re-enabled the build. The modified code still builds on GCC 5 and GCC 6.

Edited by username-removed-195241

Merge request reports