udev-util: bound leading whitespace skip in udev_replace_whitespace (#42757)

udev_replace_whitespace() is documented to read at most 'len' bytes from
'str':
- the strspn() skip of leading whitespace stops at a non-space byte or
NUL, not at 'len'
- ata_id passes the space padded, non-NUL-terminated ATA IDENTIFY
model/serial/fw fields
- an all-blank field reads off the end of the 512-byte hd_driveid stack
struct
Capped the skip to 'len'; existing outputs are unchanged. Added a
regression test.
This commit is contained in:
Yu Watanabe
2026-07-17 01:16:07 +09:00
committed by GitHub
5 changed files with 80 additions and 1 deletions
+3 -1
View File
@@ -315,7 +315,9 @@ size_t udev_replace_whitespace(const char *str, char *to, size_t len) {
*
* Note that only 'len' characters are read from 'str'. */
i = strspn(str, WHITESPACE);
/* Skip leading whitespace, but read at most 'len' bytes: 'str' is not necessarily NUL
* terminated within 'len' (e.g. the space padded ATA IDENTIFY fields ata_id passes in). */
i = strnspn(str, WHITESPACE, len);
for (j = 0; j < len && i < len && str[i] != '\0'; i++) {
if (isspace(str[i])) {