From 94736ba1a46ba90832ce0afdfbc589079e7640b6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Stefan=20Br=C3=BCns?= <stefan.bruens@rwth-aachen.de>
Date: Sun, 1 Jul 2018 01:34:35 +0200
Subject: [PATCH] Use offsetof instead of homegrown macro triggering undefined
 behaviour

Usage of &0 triggers undefined behaviour according to C/C++ standards, use
offsetof from cstddef instead, which typically uses a compiler builtin.
---
 game/StimResponse/StimResponseTimer.cpp | 6 +++---
 idlib/math/Simd_SSE2.cpp                | 4 +++-
 2 files changed, 6 insertions(+), 4 deletions(-)

diff --git a/game/StimResponse/StimResponseTimer.cpp b/game/StimResponse/StimResponseTimer.cpp
index 652bc83..f95de42 100644
--- a/game/StimResponse/StimResponseTimer.cpp
+++ b/game/StimResponse/StimResponseTimer.cpp
@@ -15,12 +15,12 @@
 #include "precompiled.h"
 #pragma hdrstop
 
-
+#include <cstddef>
 
 #include "StimResponseTimer.h"
 
-static_assert((size_t)&((TimerValue*)NULL)->Time.Millisecond == 4, "TimerValue type has wrong packing");
-static_assert((size_t)&((TimerValue*)NULL)->Val.Millisecond == 4, "TimerValue type has wrong packing");
+static_assert(offsetof(TimerValue, Time.Millisecond) == 4, "TimerValue type has wrong packing");
+static_assert(offsetof(TimerValue, Val.Millisecond) == 4, "TimerValue type has wrong packing");
 
 /********************************************************************/
 /*                 CStimResponseTimer                               */
diff --git a/idlib/math/Simd_SSE2.cpp b/idlib/math/Simd_SSE2.cpp
index ad31885..65a5cde 100644
--- a/idlib/math/Simd_SSE2.cpp
+++ b/idlib/math/Simd_SSE2.cpp
@@ -16,6 +16,8 @@
 #include "precompiled.h"
 #pragma hdrstop
 
+#include <cstddef>
+
 #include "Simd_Generic.h"
 #include "Simd_MMX.h"
 #include "Simd_SSE.h"
@@ -857,7 +859,7 @@ void VPCALL idSIMD_SSE2::MixedSoundToSamples( short *samples, const float *mixBu
 //suitable for any compiler, OS and bitness  (intrinsics)
 //generally used on Windows 64-bit and all Linuxes
 
-#define OFFSETOF(s, m) ((int)(uintptr_t)&(((s*)NULL)->m))
+#define OFFSETOF(s, m) offsetof(s, m)
 #define SHUF(i0, i1, i2, i3) _MM_SHUFFLE(i3, i2, i1, i0)
 
 #define DOT_PRODUCT(xyz, a, b) \
-- 
2.18.0

