[PATCH xserver 1/1] list: Use offsetof() to determine member offsets within a structure

Jeremy Huddleston Sequoia jeremyhu at freedesktop.org
Tue Aug 28 12:14:21 PDT 2012


This patch addresses one of two failures in the test/list test when 
built using clang with optimizations enabled.  Without this change, 
XQuartz will crash on launch due to changes introduced with 
4bf0192d810e01c89a1903cc4bc5e639fc13a547 which exposed this bug.

There is still one failure in test/list which I'm looking into.  Again, 
it seems to only appear when optimizations are enabled:
Assertion failed: (!xorg_list_is_empty(&child[2].node)), function 
test_xorg_list_del, file list.c, line 182.

--Jeremy

On 2012-08-28 10:38, Jeremy Huddleston Sequoia wrote:

> Some compilers have difficulty with the previous implementation which
> relies on undefined behavior according to the C standard. Using
> offsetof() from (which most likely just uses
> __builtin_offsetof on modern compilers) allows us to accomplish this
> without ambiguity.
>
> Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu at apple.com>
> ---
> include/list.h | 10 ++++++----
> 1 file changed, 6 insertions(+), 4 deletions(-)
>
> diff --git a/include/list.h b/include/list.h
> index d54a207..ae5431c 100644
> --- a/include/list.h
> +++ b/include/list.h
> @@ -26,6 +26,8 @@
> #ifndef _XORG_LIST_H_
> #define _XORG_LIST_H_
>
> +#include /* offsetof() */
> +
> /**
> * @file Classic doubly-link circular list implementation.
> * For real usage examples of the linked list, see the file 
> test/list.c
> @@ -232,7 +234,7 @@ xorg_list_is_empty(struct xorg_list *head)
> */
> #ifndef container_of
> #define container_of(ptr, type, member)
> - (type *)((char *)(ptr) - (char *) &((type *)0)->member)
> + (type *)((char *)(ptr) - (char *) offsetof(type, member))
> #endif
>
> /**
> @@ -271,9 +273,9 @@ xorg_list_is_empty(struct xorg_list *head)
> #define xorg_list_last_entry(ptr, type, member)
> xorg_list_entry((ptr)->prev, type, member)
>
> -#define __container_of(ptr, sample, member)
> - (void *)((char *)(ptr)
> - - ((char *)&(sample)->member - (char *)(sample)))
> +#define __container_of(ptr, sample, member)
> + container_of(ptr, typeof(*sample), member)
> +
> /**
> * Loop through the list given by head and set pos to struct in the 
> list.
> *


More information about the xorg-devel mailing list