c - HDIO_GET_IDENTITY hd_driveid.serial_no 不是 nul 终止的?
问题描述
我正在使用 HDIO_GET_IDENTITY 获取 HDD 序列号 ioctl 用所有信息填充 struct hd_driveid 结构。
但正如我所见,serial_no 字符串不是以 nul 结尾的:
(gdb) p hd.serial_no
$4 = ' ' <repeats 12 times>, "Z4Y5M5RZ"
(gdb) p hd.serial_no[17]
$5 = 53 '5'
(gdb) p hd.serial_no[18]
$6 = 82 'R'
(gdb) p hd.serial_no[19] (this should be the last byte)
$7 = 90 'Z'
(gdb)
根据定义(在 hdreg.h 中),serial_no 成员是 20 个字节的数组。这应该意味着 19 个字节的序列号字符串和 1 个字节的 nul 终止符。
但在 gdb 中,我看到序列号由 12 个空格组成,后跟 8 个字符。共 20 个字符。
现在它可以正常工作,只是因为结构中的下一个字段 (buf_type) 为零:
unsigned char serial_no[20]; /* 0 = not_specified */
unsigned short buf_type; /* Retired */
但是在 buf_type 不为零的情况下,可能会发生溢出。
更。当序列号被缩短时,它以空格为前缀,而不是在末尾有 nul-bytes(这很烦人,因为我必须修剪空格),这样就可以保证没有 nul-terminator(除了下一个成员,buf_type,当为零时)。
这是 HDIO_GET_IDENTITY 中的错误吗?我们期望 C 中的字符串以 nul 结尾并能够从中 strcpy 吗?
解决方案
推荐阅读
- neural-network - L1 regularization neural network in Pytorch does not yield sparse solution
- node.js - Mongodb:不支持选项 [...]
- c# - 如何在 C# 中打印成员名称及其值?
- python - 如何使用正则表达式函数删除所有 html 标签,包括其中的内容,如类名等?
- anaconda - Miniconda 挂在“conda 信息”上
- css - css 文件不加载 express-handlebars
- qt - Qt 默认样式表语法
- javascript - 如何使用Angular EventEmitter将数据从一个组件传递到另一个组件
- python - Python/Pandas:将当前按年-月设置的列组合成一年的平均值
- jquery - Rails 数据 id 没有选择正确的 id