udev-util: bound leading whitespace skip in udev_replace_whitespace

The function documents that at most 'len' bytes are read from 'str',
but the leading whitespace skip used strspn(), which is bounded only
by a non-whitespace byte or a NUL. ata_id passes the space padded,
non-NUL-terminated ATA IDENTIFY fields, so an all-blank model reads
past the buffer. Use strnspn() to cap the skip to 'len'.
This commit is contained in:
Syed Mohammed Nayyar
2026-07-16 15:53:37 +05:30
parent 0af6caff42
commit 6b2cb3abeb
2 changed files with 42 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])) {