在 Linux 内核中使用断言 [英] Using assertion in the Linux kernel

查看:48
本文介绍了在 Linux 内核中使用断言的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 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_ONWARN_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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆