Search code examples
linuxlinux-kernelassertassertionsassertion

Using assertion in the Linux kernel


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?


Solution

  • 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.