Using assertion in the Linux kernel

macindows picture macindows · Jun 15, 2011 · Viewed 34.5k times · Source

I have a question about assert() in Linux: can I use it in the kernel?

If no, what techniques do you usually use if, for example I don't want to enter NULL pointer?

Answer

Nemo picture Nemo · Jun 15, 2011

The corresponding kernel macros are BUG_ON and WARN_ON. The former is for when you want to make the kernel panic and bring the system down (i.e., unrecoverable error). The latter is for when you want to log something to the kernel log (viewable via dmesg).

As @Michael says, in the kernel, you need to validate anything that comes from userspace and just handle it, whatever it is. BUG_ON and WARN_ON are to catch bugs in your own code or problems with the hardware.