From: Mathieu Desnoyers Date: Tue, 3 Dec 2013 16:31:16 +0000 (+0100) Subject: Fix undefined NULL pointer arithmetic in hlist X-Git-Tag: v0.9.0~119 X-Git-Url: https://git.lttng.org./?a=commitdiff_plain;h=db903109f0031c831e8fdc95cb7197996e53f46d;p=urcu.git 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 --- 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 */