From dcb4b5bfeaf43dc8ff9c449a49ed3163d50160e7 Mon Sep 17 00:00:00 2001 From: "Marco Antonio J. Costa" Date: Tue, 28 Jul 2026 13:44:49 -0300 Subject: [PATCH] cmake: do not optimize away nullptr checks OBJECTTYPE::exists() and SOLDIERTYPE::exists()/DeleteSoldier() are called on null pointers by design and guard with `this == NULL`. That is undefined behaviour, so clang infers `this` is non-null: it deletes the guard inside the callee *and* deletes null checks that follow a call in the caller, which is an access violation at /O2 in code MSVC has always compiled the naive way. The caller-side inference happens in every translation unit that calls one of these, so this has to be global rather than per-file; there is no per-function attribute or pragma for it. Drop it once nothing relies on a null `this`. Co-Authored-By: Claude Fable 5 --- CMakeLists.txt | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/CMakeLists.txt b/CMakeLists.txt index 71a1ea209..11c28801c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -42,6 +42,10 @@ if(ADDRESS_SANITIZER) add_compile_options($,$>,-fsanitize=address,>) endif() +if(CMAKE_CXX_COMPILER_ID STREQUAL "Clang") + add_compile_options(-fno-delete-null-pointer-checks) +endif() + # whether we are using MSBuild as a generator set(usingMsBuild $)