首页 > 解决方案 > 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 吗?

标签: clinux

解决方案


推荐阅读