Commit 146afeb2 authored by Xu Panda's avatar Xu Panda Committed by Jens Axboe

block: use strscpy() to instead of strncpy()

The implementation of strscpy() is more robust and safer.
That's now the recommended way to copy NUL terminated strings.
Signed-off-by: default avatarXu Panda <xu.panda@zte.com.cn>
Signed-off-by: default avatarYang Yang <yang.yang29@zte.com>
Reviewed-by: default avatarJustin Stitt <justinstitt@google.com>
Link: https://lore.kernel.org/r/202212031422587503771@zte.com.cnSigned-off-by: default avatarJens Axboe <axboe@kernel.dk>
parent 3bfeb612
...@@ -81,8 +81,7 @@ static int parse_subpart(struct cmdline_subpart **subpart, char *partdef) ...@@ -81,8 +81,7 @@ static int parse_subpart(struct cmdline_subpart **subpart, char *partdef)
length = min_t(int, next - partdef, length = min_t(int, next - partdef,
sizeof(new_subpart->name) - 1); sizeof(new_subpart->name) - 1);
strncpy(new_subpart->name, partdef, length); strscpy(new_subpart->name, partdef, length);
new_subpart->name[length] = '\0';
partdef = ++next; partdef = ++next;
} else } else
...@@ -140,8 +139,7 @@ static int parse_parts(struct cmdline_parts **parts, const char *bdevdef) ...@@ -140,8 +139,7 @@ static int parse_parts(struct cmdline_parts **parts, const char *bdevdef)
} }
length = min_t(int, next - bdevdef, sizeof(newparts->name) - 1); length = min_t(int, next - bdevdef, sizeof(newparts->name) - 1);
strncpy(newparts->name, bdevdef, length); strscpy(newparts->name, bdevdef, length);
newparts->name[length] = '\0';
newparts->nr_subparts = 0; newparts->nr_subparts = 0;
next_subpart = &newparts->subpart; next_subpart = &newparts->subpart;
...@@ -153,8 +151,7 @@ static int parse_parts(struct cmdline_parts **parts, const char *bdevdef) ...@@ -153,8 +151,7 @@ static int parse_parts(struct cmdline_parts **parts, const char *bdevdef)
length = (!next) ? (sizeof(buf) - 1) : length = (!next) ? (sizeof(buf) - 1) :
min_t(int, next - bdevdef, sizeof(buf) - 1); min_t(int, next - bdevdef, sizeof(buf) - 1);
strncpy(buf, bdevdef, length); strscpy(buf, bdevdef, length);
buf[length] = '\0';
ret = parse_subpart(next_subpart, buf); ret = parse_subpart(next_subpart, buf);
if (ret) if (ret)
...@@ -267,8 +264,7 @@ static int add_part(int slot, struct cmdline_subpart *subpart, ...@@ -267,8 +264,7 @@ static int add_part(int slot, struct cmdline_subpart *subpart,
label_min = min_t(int, sizeof(info->volname) - 1, label_min = min_t(int, sizeof(info->volname) - 1,
sizeof(subpart->name)); sizeof(subpart->name));
strncpy(info->volname, subpart->name, label_min); strscpy(info->volname, subpart->name, label_min);
info->volname[label_min] = '\0';
snprintf(tmp, sizeof(tmp), "(%s)", info->volname); snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
strlcat(state->pp_buf, tmp, PAGE_SIZE); strlcat(state->pp_buf, tmp, PAGE_SIZE);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment