Fix undefined NULL pointer arithmetic in hlist
authorMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 3 Dec 2013 16:31:16 +0000 (17:31 +0100)
committerMathieu Desnoyers <mathieu.desnoyers@efficios.com>
Tue, 3 Dec 2013 16:31:16 +0000 (17:31 +0100)
Clang 3.3 with -O2 optimisations is especially picky about arithmetic on
NULL pointers. This undefined behavior is turned into optimized out NULL
checks by clang 3.3. Fix the undefined behavior by checking against the
pointer directly, without going back and forth around NULL with
pointer arithmetic.

Signed-off-by: Mathieu Desnoyers <mathieu.desnoyers@efficios.com>
urcu/hlist.h

index 9f4332ac87d9afd33e48e5de6dcd3c4a96d5da88..462f4937363fa0c137ae8603531ec23399ec7d10 100644 (file)
@@ -93,14 +93,18 @@ void cds_hlist_del(struct cds_hlist_node *elem)
                        entry = cds_hlist_entry(pos, __typeof__(*entry), member))
 
 #define cds_hlist_for_each_entry_2(entry, head, member) \
-       for (entry = cds_hlist_entry((head)->next, __typeof__(*entry), member); \
-               &entry->member != NULL; \
-               entry = cds_hlist_entry(entry->member.next, __typeof__(*entry), member))
+       for (entry = ((head)->next == NULL ? NULL \
+                       : cds_hlist_entry((head)->next, __typeof__(*entry), member)); \
+               entry != NULL; \
+               entry = (entry->member.next == NULL ? NULL \
+                       : cds_hlist_entry(entry->member.next, __typeof__(*entry), member)))
 
 #define cds_hlist_for_each_entry_safe_2(entry, e, head, member) \
-       for (entry = cds_hlist_entry((head)->next, __typeof__(*entry), member); \
-               (&entry->member != NULL) && (e = cds_hlist_entry(entry->member.next, \
-                                               __typeof__(*entry), member), 1); \
+       for (entry = ((head)->next == NULL ? NULL \
+                       : cds_hlist_entry((head)->next, __typeof__(*entry), member)); \
+               (entry != NULL) && (e = (entry->member.next == NULL ? NULL \
+                                       : cds_hlist_entry(entry->member.next, \
+                                               __typeof__(*entry), member), 1)); \
                entry = e)
 
 #endif /* _KCOMPAT_HLIST_H */
This page took 0.033611 seconds and 4 git commands to generate.