Using assertion in the Linux kernel

后端 未结 6 1150
挽巷
挽巷 2021-02-05 03:09

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

6条回答
  •  孤街浪徒
    2021-02-05 03:51

    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.

提交回复
热议问题