From e4ce527a389c6d7015b3c79e63d4c8d12eb81ea5 Mon Sep 17 00:00:00 2001 From: dmiller Date: Sat, 30 Nov 2013 01:32:58 +0000 Subject: [PATCH] Avoid null pointer dereference to implement container_of Using offsetof instead of actually dereferencing a 0 pointer is still standards compliant (works under gcc -ansi, clang -ansi), and is more in line with how Linux kernel defines this macro (kernel.h). Can't exactly lift the kernel implementation because it uses gnu-extension typeof. Not really a necessary change, but it was causing clang's address sanitizer to complain. --- nsock/src/gh_heap.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/nsock/src/gh_heap.h b/nsock/src/gh_heap.h index 82407aabf..654774a3e 100644 --- a/nsock/src/gh_heap.h +++ b/nsock/src/gh_heap.h @@ -73,7 +73,7 @@ #if !defined(container_of) #define container_of(ptr, type, member) \ - ((type *)((char *)(ptr)-(char *)(&((type *)0)->member))) + ((type *)((char *)(ptr) - offsetof(type, member))) #endif