Hi, I'm checking the container_of and offsetof macro #define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) );}) #define offsetof(TYPE, MEMBER) \ ((size_t) &((TYPE *)0)->MEMBER) I did not understand the first line of both macro. Will it not create the NULL pointer dereference problem? I know it works and read some article but it did not explain this part, so can anyone explain why the NULL pointer error is not coming. Am I missing something? Any C language specification of such example.
On Fri, Mar 09, 2012 at 11:51:54PM +0530, Vijay Chauhan wrote:
Hi,
I'm checking the container_of and offsetof macro
#define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) );})
#define offsetof(TYPE, MEMBER) \ ((size_t) &((TYPE *)0)->MEMBER)
I did not understand the first line of both macro. Will it not create the NULL pointer dereference problem? I know it works and read some article but it did not explain this part, so can anyone explain why the NULL pointer error is not coming. Am I missing something? Any C language specification of such example.
Did you see this: http://www.kroah.com/log/linux/container_of.html after reading that, if you have further questions, please let us know. greg k-h
I did not understand the first line of both macro. Will it not create the NULL pointer dereference problem? I know it works and read some article but it did not explain this part, so can anyone explain why the NULL pointer error is not coming. Am I missing something? Any C language specification of such example.
There is also some interesting reply (See the reply to thread from Laurent) at http://www.spinics.net/lists/linux-usb-devel/msg11766.html -- Thanks - Manish
Also, from the archives: http://www.mail-archive.com/kernelnewbies@nl.linux.org/msg12320.html On Fri, Mar 9, 2012 at 7:21 PM, Vijay Chauhan <kernel.vijay@gmail.com>wrote:
Hi,
I'm checking the container_of and offsetof macro
#define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) );})
#define offsetof(TYPE, MEMBER) \ ((size_t) &((TYPE *)0)->MEMBER)
I did not understand the first line of both macro. Will it not create the NULL pointer dereference problem? I know it works and read some article but it did not explain this part, so can anyone explain why the NULL pointer error is not coming. Am I missing something? Any C language specification of such example.
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-- /manohar
On Sat, Mar 10, 2012 at 12:10 AM, Manohar Vanga <manohar.vanga@gmail.com> wrote:
Also, from the archives: http://www.mail-archive.com/kernelnewbies@nl.linux.org/msg12320.html
Thank you for links which gives good explanation. But I am not able to understand the part accessing a member through NULL pointer like ((size_t) &((TYPE *)0)->MEMBER) How it is handled? In C code if we access a member through NULL pointer like this will cause the code to crash. Am I missing something?
On Fri, Mar 9, 2012 at 7:21 PM, Vijay Chauhan <kernel.vijay@gmail.com> wrote:
Hi,
I'm checking the container_of and offsetof macro
#define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) );})
#define offsetof(TYPE, MEMBER) \ ((size_t) &((TYPE *)0)->MEMBER)
I did not understand the first line of both macro. Will it not create the NULL pointer dereference problem? I know it works and read some article but it did not explain this part, so can anyone explain why the NULL pointer error is not coming. Am I missing something? Any C language specification of such example.
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-- /manohar
On Mon, Mar 12, 2012 at 1:03 AM, Vijay Chauhan <kernel.vijay@gmail.com> wrote:
On Sat, Mar 10, 2012 at 12:10 AM, Manohar Vanga <manohar.vanga@gmail.com> wrote:
Also, from the archives: http://www.mail-archive.com/kernelnewbies@nl.linux.org/msg12320.html
Thank you for links which gives good explanation. But I am not able to understand the part accessing a member through NULL pointer like ((size_t) &((TYPE *)0)->MEMBER) How it is handled?
This macro is interpreted at 'compile-time', i.e during the preprocessor stage...the NULL pointer exception would be generated if the code was 'run' at runtime. The typeof GCC builtin is also interpreted at compile-time. CMIIW. Hope that explains the seeming NULL pointer dereference. -mandeep
In C code if we access a member through NULL pointer like this will cause the code to crash. Am I missing something?
On Fri, Mar 9, 2012 at 7:21 PM, Vijay Chauhan <kernel.vijay@gmail.com> wrote:
Hi,
I'm checking the container_of and offsetof macro
#define container_of(ptr, type, member) ({ \ const typeof( ((type *)0)->member ) *__mptr = (ptr); (type *)( (char *)__mptr - offsetof(type,member) );})
#define offsetof(TYPE, MEMBER) \ ((size_t) &((TYPE *)0)->MEMBER)
I did not understand the first line of both macro. Will it not create the NULL pointer dereference problem? I know it works and read some article but it did not explain this part, so can anyone explain why the NULL pointer error is not coming. Am I missing something? Any C language specification of such example.
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
-- /manohar
_______________________________________________ Kernelnewbies mailing list Kernelnewbies@kernelnewbies.org http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
On Sun, Mar 11, 2012 at 9:40 PM, Mandeep Sandhu <mandeepsandhu.chd@gmail.com> wrote:
On Mon, Mar 12, 2012 at 1:03 AM, Vijay Chauhan <kernel.vijay@gmail.com> wrote:
On Sat, Mar 10, 2012 at 12:10 AM, Manohar Vanga <manohar.vanga@gmail.com> wrote:
Also, from the archives: http://www.mail-archive.com/kernelnewbies@nl.linux.org/msg12320.html
Thank you for links which gives good explanation. But I am not able to understand the part accessing a member through NULL pointer like ((size_t) &((TYPE *)0)->MEMBER) How it is handled?
All you are doing is computing the address and not dereferencing it. If you dereference it, yes it will panic. eg.. *(((size_t) &((TYPE *)0)->MEMBER)) will cause panic -- Thanks - Manish
On Mon, Mar 12, 2012 at 10:19 AM, Manish Katiyar <mkatiyar@gmail.com> wrote:
On Sun, Mar 11, 2012 at 9:40 PM, Mandeep Sandhu <mandeepsandhu.chd@gmail.com> wrote:
On Mon, Mar 12, 2012 at 1:03 AM, Vijay Chauhan <kernel.vijay@gmail.com> wrote:
On Sat, Mar 10, 2012 at 12:10 AM, Manohar Vanga <manohar.vanga@gmail.com> wrote:
Also, from the archives: http://www.mail-archive.com/kernelnewbies@nl.linux.org/msg12320.html
Thank you for links which gives good explanation. But I am not able to understand the part accessing a member through NULL pointer like ((size_t) &((TYPE *)0)->MEMBER) How it is handled?
All you are doing is computing the address and not dereferencing it. If you dereference it, yes it will panic. eg..
*(((size_t) &((TYPE *)0)->MEMBER)) will cause panic
OK I got it. Thank you all for your help. Vijay
-- Thanks - Manish
participants (5)
-
Greg KH -
Mandeep Sandhu -
Manish Katiyar -
Manohar Vanga -
Vijay Chauhan