From db903109f0031c831e8fdc95cb7197996e53f46d Mon Sep 17 00:00:00 2001 From: Mathieu Desnoyers Date: Tue, 3 Dec 2013 17:31:16 +0100 Subject: [PATCH] Fix undefined NULL pointer arithmetic in hlist 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 --- urcu/hlist.h | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/urcu/hlist.h b/urcu/hlist.h index 9f4332a..462f493 100644 --- a/urcu/hlist.h +++ b/urcu/hlist.h @@ -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 */ -- 2.34.1