From 82c6804516e1676c2ad76c1b6368e79d510c49e3 Mon Sep 17 00:00:00 2001 From: PatR Date: Fri, 28 Feb 2025 10:38:50 -0800 Subject: [PATCH] X11: avoid null-pointer-subtraction warnings Most recent version of XQuartz, same as before. Unfortunately, newer version of macOS => newer version of Xcode and its command line tools => newer version of clang => emulating newer version of gcc which defaults to a more recent version of StdC, I suppose, or perhaps our hints are specifying that. Whichever, it has resulted in a bunch of complaints about XtOffset() used in win/X11/winX.c: |warning: performing pointer subtraction with a null pointer has\ undefined behavior [-Wnull-pointer-subtraction] Adding -wno-null-pointer-subtraction to X11FLAGS silences them, but that would require figuring out which versions of gcc and clang added -Wnull-pointer-subtraction and its negation. Revising XtOffset() to include the ptrdiff_t casts eliminates the warnings, avoiding the need for version conditionals to deal with X11FLAGS. --- include/winX.h | 15 ++++++++++++++- 1 file changed, 14 insertions(+), 1 deletion(-) diff --git a/include/winX.h b/include/winX.h index 3da329d7d..496412edc 100644 --- a/include/winX.h +++ b/include/winX.h @@ -1,4 +1,4 @@ -/* NetHack 3.7 winX.h $NHDT-Date: 1643491525 2022/01/29 21:25:25 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.52 $ */ +/* NetHack 3.7 winX.h $NHDT-Date: 1740795096 2025/02/28 18:11:36 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.65 $ */ /* Copyright (c) Dean Luick, 1992 */ /* NetHack may be freely redistributed. See license for details. */ @@ -26,6 +26,19 @@ #endif #endif +/* winX.c uses XtOffset() and the way that that macro is defined in + triggers "performing pointer subtraction with + a null pointer has undefined behavior" warnings; this modified + edition doesn't guarantee defined behavior but does silence those + warnings without needing to know whether current compiler version + supports the '-wno-null-pointer-subtraction' option */ +#ifdef XtOffset +#undef XtOffset +#define XtOffset(p_type,field) \ + ((Cardinal) (((ptrdiff_t) (char *) (&(((p_type) NULL)->field))) \ + - ((ptrdiff_t) (char *) NULL))) +#endif + /* * Generic text buffer. */