在Linux内核中使用断言 [英] Using assertion in the Linux kernel
问题描述
我在Linux中有一个关于assert()
的问题:我可以在内核中使用它吗?
I have a question about assert()
in Linux: can I use it in the kernel?
如果没有,例如,如果我不想输入NULL指针,您通常使用什么技术?
If no, what techniques do you usually use if, for example I don't want to enter NULL pointer?
推荐答案
相应的内核宏是BUG_ON
和WARN_ON
.前者适用于您希望使内核崩溃并导致系统崩溃(即,不可恢复的错误)的情况.后者用于当您要将某些内容记录到内核日志中时(可通过dmesg
进行查看).
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
).
正如@Michael所说的,在内核中,您需要验证来自用户空间的任何内容,并处理,无论它是什么. BUG_ON和WARN_ON将捕获您自己的代码中的错误或硬件问题.
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.
这篇关于在Linux内核中使用断言的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!