[PATCH] remove null dereference from netlink/link.h

Nick Lewycky nlewycky at google.com
Thu Feb 11 12:38:38 PST 2016


On Thu, Jan 28, 2016 at 07:49:31PM -0800, Nick Lewycky wrote:
> Replace a null pointer dereference with a use of the 'offsetof' macro in stddef.h.
> 
> Signed-off-by: Nick Lewycky <nlewycky at google.com>
> ---
>  include/netlink/list.h | 4 +++-
>  1 file changed, 3 insertions(+), 1 deletion(-)
> 
> diff --git a/include/netlink/list.h b/include/netlink/list.h
> index 28712ed..fcfb826 100644
> --- a/include/netlink/list.h
> +++ b/include/netlink/list.h
> @@ -12,6 +12,8 @@
>  #ifndef NETLINK_LIST_H_
>  #define NETLINK_LIST_H_
>  
> +#include <stddef.h>
> +
>  struct nl_list_head
>  {
>  	struct nl_list_head *	next;
> @@ -59,7 +61,7 @@ static inline int nl_list_empty(struct nl_list_head *head)
>  
>  #define nl_container_of(ptr, type, member) ({			\
>          const typeof( ((type *)0)->member ) *__mptr = (ptr);	\
> -        (type *)( (char *)__mptr - ((size_t) &((type *)0)->member));})
> +        (type *)( (char *)__mptr - (offsetof(type, member)));})
>  
>  #define nl_list_entry(ptr, type, member) \
>  	nl_container_of(ptr, type, member)
> -- 
> 2.7.0.rc3.207.g0ac5344

Ping!

This patch fixes a UBSan complaint in libnl. Right now ubsan flags the null dereference in any execution of this macro. This article does a good job of describing ubsan: http://developerblog.redhat.com/2014/10/16/gcc-undefined-behavior-sanitizer-ubsan/

My patch is that doesn't touch the nearby null dereference inside typeof(). That doesn't need to be changed because that expression won't be evaluated. The exact wording in the gcc manual is "The operand of typeof is evaluated for its side effects if and only if it is an expression of variably modified type or the name of such a type".

Please review!

Nick



More information about the libnl mailing list