From a349cc8bdcc3a083ea507dbbdeba9053e3a338e4 Mon Sep 17 00:00:00 2001
From: Sebastian Lackner <sebastian@fds-team.de>
Date: Tue, 22 Mar 2016 23:08:30 +0100
Subject: [PATCH] include: Check element type in CONTAINING_RECORD and similar
 macros.

---
 include/wine/list.h   | 10 ++++++++--
 include/wine/rbtree.h | 10 ++++++++--
 include/winnt.h       | 10 ++++++++--
 3 files changed, 24 insertions(+), 6 deletions(-)

diff --git a/include/wine/list.h b/include/wine/list.h
index b4d681fe0f3..287ad394fae 100644
--- a/include/wine/list.h
+++ b/include/wine/list.h
@@ -228,7 +228,13 @@ static inline void list_move_head( struct list *dst, struct list *src )
 
 /* get pointer to object containing list element */
 #undef LIST_ENTRY
-#define LIST_ENTRY(elem, type, field) \
-    ((type *)((char *)(elem) - offsetof(type, field)))
+#ifdef __GNUC__
+# define LIST_ENTRY(elem, type, field) ({               \
+     const typeof(((type *)0)->field) *__ptr = (elem);  \
+     (type *)((char *)__ptr - offsetof(type, field)); })
+#else
+# define LIST_ENTRY(elem, type, field) \
+     ((type *)((char *)(elem) - offsetof(type, field)))
+#endif
 
 #endif  /* __WINE_SERVER_LIST_H */
diff --git a/include/wine/rbtree.h b/include/wine/rbtree.h
index 8aae29c8c10..330b3e8fbc9 100644
--- a/include/wine/rbtree.h
+++ b/include/wine/rbtree.h
@@ -23,8 +23,14 @@
 #ifndef __WINE_WINE_RBTREE_H
 #define __WINE_WINE_RBTREE_H
 
-#define WINE_RB_ENTRY_VALUE(element, type, field) \
-    ((type *)((char *)(element) - offsetof(type, field)))
+#ifdef __GNUC__
+# define WINE_RB_ENTRY_VALUE(element, type, field) ({       \
+     const typeof(((type *)0)->field) *__ptr = (element);   \
+     (type *)((char *)__ptr - offsetof(type, field)); })
+#else
+# define WINE_RB_ENTRY_VALUE(element, type, field) \
+     ((type *)((char *)(element) - offsetof(type, field)))
+#endif
 
 struct wine_rb_entry
 {
diff --git a/include/winnt.h b/include/winnt.h
index 46e17c546a7..d5c65d2017b 100644
--- a/include/winnt.h
+++ b/include/winnt.h
@@ -793,8 +793,14 @@ typedef struct _MEMORY_BASIC_INFORMATION
 #define RTL_FIELD_SIZE(type, field) (sizeof(((type *)0)->field))
 #define RTL_SIZEOF_THROUGH_FIELD(type, field) (FIELD_OFFSET(type, field) + RTL_FIELD_SIZE(type, field))
 
-#define CONTAINING_RECORD(address, type, field) \
-  ((type *)((PCHAR)(address) - offsetof(type, field)))
+#ifdef __GNUC__
+# define CONTAINING_RECORD(address, type, field) ({     \
+   const typeof(((type *)0)->field) *__ptr = (address); \
+   (type *)((PCHAR)__ptr - offsetof(type, field)); })
+#else
+# define CONTAINING_RECORD(address, type, field) \
+   ((type *)((PCHAR)(address) - offsetof(type, field)))
+#endif
 
 #define ARRAYSIZE(x) (sizeof(x) / sizeof((x)[0]))
 #ifdef __WINESRC__
-- 
2.26.2

