From 545f3d1a04808df66cbe2e024d415ea7eaef28b2 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Sat, 19 Oct 2024 16:48:23 +0200 Subject: [PATCH 1/2] Don't fiddle with NDEBUG in C code It is way to late to do this in php.h, since assert.h has already been included. Even pushing that down to zend_portability.h may not have the desired effect. Instead we define or undefine NDEBUG as CFLAG, so that it works in all circumstances. As a last resort we fail at build time, if `NDEBUG` is defined when `ZEND_DEBUG` is enabled. We also remove the useless workaround in zend_test to include assert.h again, since that usually won't have any effect anyway. --- Zend/zend_portability.h | 3 +++ configure.ac | 6 ++++++ ext/zend_test/test.c | 6 ------ main/php.h | 7 ------- win32/build/confutils.js | 2 +- 5 files changed, 10 insertions(+), 14 deletions(-) diff --git a/Zend/zend_portability.h b/Zend/zend_portability.h index f4609428326c7..e292d993219ca 100644 --- a/Zend/zend_portability.h +++ b/Zend/zend_portability.h @@ -47,6 +47,9 @@ #include "../TSRM/TSRM.h" #include +#if ZEND_DEBUG && defined(NDEBUG) +# error "NDEBUG must not be defined when ZEND_DEBUG is enabled" +#endif #include #include diff --git a/configure.ac b/configure.ac index 56c22f1a80f5d..3d13061f80159 100644 --- a/configure.ac +++ b/configure.ac @@ -799,6 +799,8 @@ PHP_ARG_ENABLE([debug], AS_VAR_IF([PHP_DEBUG], [yes], [ PHP_DEBUG=1 ZEND_DEBUG=yes + CFLAGS="$CFLAGS -U NDEBUG" + CXXFLAGS="$CFLAGS -U NDEBUG" PHP_REMOVE_OPTIMIZATION_FLAGS dnl Add -O0 only if GCC or ICC is used. if test "$GCC" = "yes" || test "$ICC" = "yes"; then @@ -817,6 +819,8 @@ AS_VAR_IF([PHP_DEBUG], [yes], [ ], [ PHP_DEBUG=0 ZEND_DEBUG=no + CFLAGS="$CFLAGS -D NDEBUG" + CXXFLAGS="$CFLAGS -D NDEBUG" ]) PHP_ARG_ENABLE([debug-assertions], @@ -829,6 +833,8 @@ PHP_ARG_ENABLE([debug-assertions], AS_VAR_IF([PHP_DEBUG_ASSERTIONS], [yes], [ PHP_DEBUG=1 ZEND_DEBUG=yes + CFLAGS="$CFLAGS -U NDEBUG" + CXXFLAGS="$CFLAGS -U NDEBUG" ]) AC_ARG_ENABLE([zts], diff --git a/ext/zend_test/test.c b/ext/zend_test/test.c index 37b3f7a2c5e49..701077573d8dc 100644 --- a/ext/zend_test/test.c +++ b/ext/zend_test/test.c @@ -38,12 +38,6 @@ #include "zend_exceptions.h" #include "zend_mm_custom_handlers.h" -// `php.h` sets `NDEBUG` when not `PHP_DEBUG` which will make `assert()` from -// assert.h a no-op. In order to have `assert()` working on NDEBUG builds, we -// undefine `NDEBUG` and re-include assert.h -#undef NDEBUG -#include "assert.h" - #if defined(HAVE_LIBXML) && !defined(PHP_WIN32) # include # include diff --git a/main/php.h b/main/php.h index c00777a1b40a0..a7ff4b6e35bea 100644 --- a/main/php.h +++ b/main/php.h @@ -109,13 +109,6 @@ typedef int pid_t; # endif #endif -#if PHP_DEBUG -#undef NDEBUG -#else -#ifndef NDEBUG -#define NDEBUG -#endif -#endif #include #ifdef HAVE_UNIX_H diff --git a/win32/build/confutils.js b/win32/build/confutils.js index be4ee520fe517..3623dcf7e25e1 100644 --- a/win32/build/confutils.js +++ b/win32/build/confutils.js @@ -3429,7 +3429,7 @@ function toolset_setup_common_libs() function toolset_setup_build_mode() { if (PHP_DEBUG == "yes") { - ADD_FLAG("CFLAGS", "/LDd /MDd /Od /D ZEND_DEBUG=1 " + + ADD_FLAG("CFLAGS", "/LDd /MDd /Od /U NDebug /U NDEBUG /D ZEND_DEBUG=1 " + (TARGET_ARCH == 'x86'?"/ZI":"/Zi")); ADD_FLAG("LDFLAGS", "/debug"); // Avoid problems when linking to release libraries that use the release From 9d150253b96aa2693fbf4eeb65af85e93218c6a9 Mon Sep 17 00:00:00 2001 From: "Christoph M. Becker" Date: Mon, 21 Oct 2024 14:12:28 +0200 Subject: [PATCH 2/2] fix Co-authored-by: Arnaud Le Blanc --- configure.ac | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/configure.ac b/configure.ac index 3d13061f80159..01d9ded69b920 100644 --- a/configure.ac +++ b/configure.ac @@ -799,8 +799,8 @@ PHP_ARG_ENABLE([debug], AS_VAR_IF([PHP_DEBUG], [yes], [ PHP_DEBUG=1 ZEND_DEBUG=yes - CFLAGS="$CFLAGS -U NDEBUG" - CXXFLAGS="$CFLAGS -U NDEBUG" + CFLAGS="$CFLAGS -UNDEBUG" + CXXFLAGS="$CXXFLAGS -UNDEBUG" PHP_REMOVE_OPTIMIZATION_FLAGS dnl Add -O0 only if GCC or ICC is used. if test "$GCC" = "yes" || test "$ICC" = "yes"; then @@ -819,8 +819,8 @@ AS_VAR_IF([PHP_DEBUG], [yes], [ ], [ PHP_DEBUG=0 ZEND_DEBUG=no - CFLAGS="$CFLAGS -D NDEBUG" - CXXFLAGS="$CFLAGS -D NDEBUG" + CFLAGS="$CFLAGS -DNDEBUG" + CXXFLAGS="$CXXFLAGS -DNDEBUG" ]) PHP_ARG_ENABLE([debug-assertions], @@ -833,8 +833,8 @@ PHP_ARG_ENABLE([debug-assertions], AS_VAR_IF([PHP_DEBUG_ASSERTIONS], [yes], [ PHP_DEBUG=1 ZEND_DEBUG=yes - CFLAGS="$CFLAGS -U NDEBUG" - CXXFLAGS="$CFLAGS -U NDEBUG" + CFLAGS="$CFLAGS -UNDEBUG" + CXXFLAGS="$CXXFLAGS -UNDEBUG" ]) AC_ARG_ENABLE([zts],