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?
This macro can help programmers find bugs in their programs, or handle exceptional cases via a crash that will produce limited debugging output. If expression is false (i.e., compares equal to zero), assert() prints an error message to standard error and terminates the program by calling abort(3).
BUG_ON and WARN_ON are to catch bugs in your own code or problems with the hardware.
BUG() and BUG_ON(condition) are used as a debugging help when something in the kernel goes terribly wrong. When a BUG_ON() assertion fails, or the code takes a branch with BUG() in it, the kernel will print out the contents of the registers and a stack trace. After that the current process will die.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With