Skip to content

Commit

Permalink
Server: Backport 0060-Check-C23-nullptr-usability-as-a-sentinel.patch
Browse files Browse the repository at this point in the history
Signed-off-by: Marko Lindqvist <[email protected]>
  • Loading branch information
cazfi committed Apr 22, 2024
1 parent 49d462a commit 6434e2d
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
4 changes: 4 additions & 0 deletions freeciv/apply_patches.sh
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,9 @@
# 0049-Trigger-action-system-when-client-requests-activity-.patch
# Activity action fix (esp. Pillage)
# osdn #57670
# 0060-Check-C23-nullptr-usability-as-a-sentinel.patch
# C23 compile fix
# RM #475

# Not in the upstream Freeciv server
# ----------------------------------
Expand Down Expand Up @@ -61,6 +64,7 @@ declare -a PATCHLIST=(
"backports/0050-savegame-Fix-loading-invalid-resources-on-FoW-map"
"backports/0054-Fix-inconsistent-city-workers-after-vision-loss"
"backports/0049-Trigger-action-system-when-client-requests-activity-"
"backports/0060-Check-C23-nullptr-usability-as-a-sentinel"
"RevertAmplio2ExtraUnits"
"meson_webperimental"
"metachange"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
From ac5ef97b9a6c833011278db8eeb845774062f046 Mon Sep 17 00:00:00 2001
From: Marko Lindqvist <[email protected]>
Date: Fri, 19 Apr 2024 17:06:10 +0300
Subject: [PATCH 60/60] Check C23 nullptr usability as a sentinel

See RM #475

Signed-off-by: Marko Lindqvist <[email protected]>
---
m4/c23.m4 | 21 +++++++++++++--------
meson.build | 9 ++++++---
2 files changed, 19 insertions(+), 11 deletions(-)

diff --git a/m4/c23.m4 b/m4/c23.m4
index 0c57f04856..9a265a0065 100644
--- a/m4/c23.m4
+++ b/m4/c23.m4
@@ -1,18 +1,23 @@
# Check for the presence of C23 features.

-# Check for C23 nullptr, and define FREECIV_HAVE_C23_NULLPTR if it's available
+# Check for C23 nullptr that can be passed as sentinel
+# and define FREECIV_HAVE_C23_NULLPTR if such is available
#
AC_DEFUN([FC_C23_NULLPTR],
[
AC_LANG_PUSH([C])
- AC_CHECK_HEADERS([stddef])
+ AC_CHECK_HEADERS([stddef.h])
AC_CACHE_CHECK([for C23 nullptr], [ac_cv_c23_nullptr],
- [AC_LINK_IFELSE([AC_LANG_PROGRAM(
-[[#ifdef HAVE_STDDEF
-#include <stddef>
-#endif]],
- [[ int *var = nullptr; ]])],
-[ac_cv_c23_nullptr=yes], [ac_cv_c23_nullptr=no])])
+ [_cflags_="$CFLAGS"
+ CFLAGS="$EXTRA_DEBUG_CFLAGS $CFLAGS"
+ AC_COMPILE_IFELSE([AC_LANG_PROGRAM(
+[[#ifdef HAVE_STDDEF_H
+#include <stddef.h>
+#endif
+void sentinental(...) __attribute__((__sentinel__(0)));]],
+ [[ sentinental(nullptr); ]])],
+[ac_cv_c23_nullptr=yes], [ac_cv_c23_nullptr=no])]
+ CFLAGS="$_cflags_")
if test "x${ac_cv_c23_nullptr}" = "xyes" ; then
AC_DEFINE([FREECIV_HAVE_C23_NULLPTR], [1], [C23 nullptr available])
fi
diff --git a/meson.build b/meson.build
index 0a0b904599..c6b7483303 100644
--- a/meson.build
+++ b/meson.build
@@ -585,11 +585,14 @@ if zstd_dep.found()
pub_conf_data.set('FREECIV_HAVE_LIBZSTD', 1)
endif

+# Check for c23 nullptr that can be passed as sentinel
if c_compiler.compiles('''
-#include <stddef>
-int main(void) { int *var = nullptr; return 0; }''',
+#include <stddef.h>
+void sentinental(...) __attribute__((__sentinel__(0)));
+void caller(void) { sentinental(nullptr); }''',
name: 'c23 nullptr',
- include_directories: include_directories(cross_inc_path))
+ include_directories: include_directories(cross_inc_path),
+ args: ['-Wformat'])
pub_conf_data.set('FREECIV_HAVE_C23_NULLPTR', 1)
endif

--
2.43.0

0 comments on commit 6434e2d

Please sign in to comment.