]> git.ipfire.org Git - thirdparty/util-linux.git/commitdiff
include/list: fix undefined behavior in list_entry() macro
authorSami Liedes <sami.liedes@iki.fi>
Mon, 17 Dec 2012 15:22:35 +0000 (16:22 +0100)
committerKarel Zak <kzak@redhat.com>
Mon, 17 Dec 2012 15:23:35 +0000 (16:23 +0100)
Update list_entry() macro, which is basically the same as the
container_of() macro in the kernel, to use offsetof() to fix undefined
behavior.

Caught using clang -fsanitize=undefined.

[kzak@redhat.com: port from e2fsprogs]

Signed-off-by: Sami Liedes <sami.liedes@iki.fi>
Signed-off-by: Karel Zak <kzak@redhat.com>
include/list.h

index 1824af05d296a6bf3b1a59e5cf66a457a790610c..414d356ba16854d28090de2430fbeede68f69b59 100644 (file)
@@ -163,8 +163,9 @@ _INLINE_ void list_splice(struct list_head *list, struct list_head *head)
  * @type:      the type of the struct this is embedded in.
  * @member:    the name of the list_struct within the struct.
  */
-#define list_entry(ptr, type, member) \
-       ((type *)((char *)(ptr)-(unsigned long)(&((type *)0)->member)))
+#define list_entry(ptr, type, member) ({              \
+       const typeof( ((type *)0)->member ) *__mptr = (ptr);   \
+       (type *)( (char *)__mptr - offsetof(type,member) );})
 
 
 #define list_first_entry(head, type, member) \