lib/list: Fix handling of empty lists
authorJeremy Kerr <jk@ozlabs.org>
Wed, 1 May 2013 05:11:17 +0000 (13:11 +0800)
committerGeoff Levand <geoff@infradead.org>
Fri, 3 May 2013 23:20:29 +0000 (16:20 -0700)
The current list_for_each_entry_safe marco SEGVs on empty lists; the
list_entry will give us a NULL tmp on the first iteration.

This change removes the use of list_entry, as we're effectively
by-passing its NULL return semantics with our own.

Signed-off-by: Jeremy Kerr <jk@ozlabs.org>
Signed-off-by: Geoff Levand <geoff@infradead.org>
lib/list/list.h

index 6000320da3e38ee1e05f4b7e7298d91599481d97..0174b90e21b281edf4adba6c8bd7ef3d36b2f3e0 100644 (file)
@@ -41,11 +41,12 @@ struct list {
        for (; _pos; _pos = list_next_entry(_list, _pos, _member))
 
 #define list_for_each_entry_safe(_list, _pos, _tmp, _member) \
-       for (_pos = list_entry((_list)->head.next, typeof(*_pos), _member, _list), \
-               _tmp = list_entry(_pos->_member.next, typeof(*_pos), _member, _list); \
-       _pos; \
-       _pos = _tmp, \
-       _tmp = _tmp ? list_entry(_tmp->_member.next, typeof(*_pos), _member, _list) : NULL)
+       for (_pos = container_of((_list)->head.next, typeof(*_pos), _member), \
+               _tmp = container_of(_pos->_member.next, typeof(*_pos), \
+                               _member); \
+            &_pos->_member != &(_list)->head; \
+            _pos = _tmp, \
+            _tmp = container_of(_tmp->_member.next, typeof(*_pos), _member))
 
 #define DEFINE_LIST(_list) struct list _list = { \
        .head = { \