我正在学习 Linux 内核开发。我已经克隆到 Linux 的稳定分支。我在 youtube 上观看了 Greg KH 的教程,其中解释了如何使用 checkpatch.pl 脚本查找不符合当前标准的代码。我只是个初学者,所以我没有想好要测试哪个文件。我决定测试启动一切的文件:init/ 中的 main.c:
./scripts/checkpatch.pl --file --terse init/main.c
输出是 main.c 文件中各种错误的一长串列表:
homie@vmi2410184:~/git/kernels/staging$ ./scripts/checkpatch.pl --file --terse init/main.c
Traceback (most recent call last):
File "/home/homie/git/kernels/staging/scripts/spdxcheck.py", line 6, in <module>
from ply import lex, yacc
ModuleNotFoundError: No module named 'ply'
init/main.c:3: WARNING: It's generally not useful to have the filename in the file
init/main.c:107: WARNING: Use #include <linux/io.h> instead of <asm/io.h>
init/main.c:110: WARNING: Use #include <linux/cacheflush.h> instead of <asm/cacheflush.h>
init/main.c:203: WARNING: Missing a blank line after declarations
init/main.c:208: WARNING: Block comments use a trailing */ on a separate line
init/main.c:401: WARNING: braces {} are not necessary for single statement blocks
init/main.c:469: WARNING: void function return statements are not generally useful
init/main.c:567: WARNING: Missing a blank line after declarations
init/main.c:579: WARNING: Missing a blank line after declarations
init/main.c:659: WARNING: Prefer strscpy over strcpy - see: https://github.com/KSPP/linux/issues/88
init/main.c:660: WARNING: Prefer strscpy over strcpy - see: https://github.com/KSPP/linux/issues/88
init/main.c:662: WARNING: Prefer strscpy over strcpy - see: https://github.com/KSPP/linux/issues/88
init/main.c:663: WARNING: Prefer strscpy over strcpy - see: https://github.com/KSPP/linux/issues/88
init/main.c:675: WARNING: Prefer strscpy over strcpy - see: https://github.com/KSPP/linux/issues/88
init/main.c:677: WARNING: Prefer strscpy over strcpy - see: https://github.com/KSPP/linux/issues/88
init/main.c:681: WARNING: Prefer strscpy over strcpy - see: https://github.com/KSPP/linux/issues/88
init/main.c:683: WARNING: Prefer strscpy over strcpy - see: https://github.com/KSPP/linux/issues/88
init/main.c:1158: WARNING: Prefer strscpy over strcpy - see: https://github.com/KSPP/linux/issues/88
init/main.c:1196: WARNING: Prefer using '"%s...", __func__' to using 'initcall_blacklist', this function's name, in a string
init/main.c:1212: WARNING: Prefer [subsystem eg: netdev]_dbg([subsystem]dev, ... then dev_dbg(dev, ... then pr_debug(... to printk(KERN_DEBUG ...
init/main.c:1222: WARNING: Prefer [subsystem eg: netdev]_dbg([subsystem]dev, ... then dev_dbg(dev, ... then pr_debug(... to printk(KERN_DEBUG ...
init/main.c:1299: ERROR: Use of const init definition must use __initconst
init/main.c:1339: WARNING: Possible unnecessary 'out of memory' message
init/main.c:1343: WARNING: Prefer strscpy over strcpy - see: https://github.com/KSPP/linux/issues/88
init/main.c:1529: WARNING: quoted string split across lines
total: 1 errors, 24 warnings, 1603 lines checked
这可能是一个愚蠢的问题。为什么这些问题还没有解决?
其中最容易改变的是将所有 strcpy 改为 strscpy。请记住 strscpy 比 strcpy 多接受一个参数。
内核开发实践随着时间的推移而发展,但是内核本身太大,无法快速将这些更改应用到所有内核。
checkpatch.pl
用于检查内核的建议补丁,而不是现有代码 - 这可确保内核逐步改进,但正如您所发现的,这对于不经常触及的代码没有帮助。从输出中引用的问题可以看出,迁移到
strscpy
正在积极进行中,补丁定期合并。针对 的补丁init/main.c
可能会受到欢迎;如果需要,请抄送 Kees Cook 以获得审核帮助。您仍需确保更改正确且值得。由于已声明的目标是消除
strcpy
use,因此替换 that 很有可能被接受。 所提倡的其他一些更改checkpatch.pl
(例如注释结构、空行和不必要的括号)不太可能被接受,除非您出于其他(有效)原因更改受影响的行。