summaryrefslogtreecommitdiff
path: root/cogl/cogl-list.h
diff options
context:
space:
mode:
authorNeil Roberts <neil@linux.intel.com>2014-02-03 20:14:48 +0000
committerNeil Roberts <neil@linux.intel.com>2014-02-19 17:24:10 +0000
commit1efed1e0a2bce706eb4901979ed4e717bb13e4e2 (patch)
treeaa5d55bbf5790a5b2cb55768ef46af8b7e872ec7 /cogl/cogl-list.h
parent4446ff89dc63a3d713cc750bf102db3f9fdea0ae (diff)
downloadcogl-1efed1e0a2bce706eb4901979ed4e717bb13e4e2.tar.gz
Don't dereference an unitialised pointer in _cogl_container_of
The previous implementation was dereferencing the sample pointer in order to get the offset to subtract from the member pointer. The resulting value is then only used to get a pointer to the member in order to calculate the offset so it doesn't actually read from the memory location and shouldn't cause any problems. However this is probably technically invalid and could have undefined behaviour. It looks like clang takes advantage of this undefined behaviour and doesn't actually offset the pointer. It also generates a warning when it does this. This patch splits the _cogl_container_of macro into two implementations. Previously the macro was always used in the list iterator macros like this: SomeType *sample = _cogl_container_of(list_node, sample, link) Instead of doing that there is now a new macro called _cogl_list_set_iterator which explicitly assigns to the sample pointer with an initial value before assigning to it again with the real offset. This redundant initialisation gets optimised out by compiler. The second macro is still called _cogl_container_of but instead of taking a sample pointer it just directly takes the type name. That way it can use the standard offsetof macro. https://bugzilla.gnome.org/show_bug.cgi?id=723530 Reviewed-by: Robert Bragg <robert@linux.intel.com>
Diffstat (limited to 'cogl/cogl-list.h')
-rw-r--r--cogl/cogl-list.h48
1 files changed, 27 insertions, 21 deletions
diff --git a/cogl/cogl-list.h b/cogl/cogl-list.h
index 7d716a89..20cbec82 100644
--- a/cogl/cogl-list.h
+++ b/cogl/cogl-list.h
@@ -26,6 +26,8 @@
#ifndef COGL_LIST_H
#define COGL_LIST_H
+#include <stddef.h>
+
/**
* CoglList - linked list
*
@@ -84,40 +86,44 @@ void
_cogl_list_insert_list (CoglList *list,
CoglList *other);
-#ifdef __GNUC__
-#define _cogl_container_of(ptr, sample, member) \
- (__typeof__(sample))((char *)(ptr) - \
- ((char *)&(sample)->member - (char *)(sample)))
-#else
-#define _cogl_container_of(ptr, sample, member) \
- (void *)((char *)(ptr) - \
- ((char *)&(sample)->member - (char *)(sample)))
-#endif
+/* This assigns to iterator first so that taking a reference to it
+ * later in the second step won't be an undefined operation. It
+ * assigns the value of list_node rather than 0 so that it is possible
+ * have list_node be based on the previous value of iterator. In that
+ * respect iterator is just used as a convenient temporary variable.
+ * The compiler optimises all of this down to a single subtraction by
+ * a constant */
+#define _cogl_list_set_iterator(list_node, iterator, member) \
+ ((iterator) = (void *) (list_node), \
+ (iterator) = (void *) ((char *) (iterator) - \
+ (((char *) &(iterator)->member) - \
+ (char *) (iterator))))
+
+#define _cogl_container_of(ptr, type, member) \
+ (type *) ((char *) (ptr) - offsetof (type, member))
#define _cogl_list_for_each(pos, head, member) \
- for (pos = 0, pos = _cogl_container_of((head)->next, pos, member); \
+ for (_cogl_list_set_iterator ((head)->next, pos, member); \
&pos->member != (head); \
- pos = _cogl_container_of(pos->member.next, pos, member))
+ _cogl_list_set_iterator (pos->member.next, pos, member))
#define _cogl_list_for_each_safe(pos, tmp, head, member) \
- for (pos = 0, tmp = 0, \
- pos = _cogl_container_of((head)->next, pos, member), \
- tmp = _cogl_container_of((pos)->member.next, tmp, member); \
+ for (_cogl_list_set_iterator ((head)->next, pos, member), \
+ _cogl_list_set_iterator ((pos)->member.next, tmp, member); \
&pos->member != (head); \
pos = tmp, \
- tmp = _cogl_container_of(pos->member.next, tmp, member))
+ _cogl_list_set_iterator (pos->member.next, tmp, member))
#define _cogl_list_for_each_reverse(pos, head, member) \
- for (pos = 0, pos = _cogl_container_of((head)->prev, pos, member); \
+ for (_cogl_list_set_iterator ((head)->prev, pos, member); \
&pos->member != (head); \
- pos = _cogl_container_of(pos->member.prev, pos, member))
+ _cogl_list_set_iterator (pos->member.prev, pos, member))
#define _cogl_list_for_each_reverse_safe(pos, tmp, head, member) \
- for (pos = 0, tmp = 0, \
- pos = _cogl_container_of((head)->prev, pos, member), \
- tmp = _cogl_container_of((pos)->member.prev, tmp, member); \
+ for (_cogl_list_set_iterator ((head)->prev, pos, member), \
+ _cogl_list_set_iterator ((pos)->member.prev, tmp, member); \
&pos->member != (head); \
pos = tmp, \
- tmp = _cogl_container_of(pos->member.prev, tmp, member))
+ _cogl_list_set_iterator (pos->member.prev, tmp, member))
#endif /* COGL_LIST_H */