Skip to content

Commit 146afeb

Browse files
Xu Pandaaxboe
authored andcommitted
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: Xu Panda <[email protected]> Signed-off-by: Yang Yang <[email protected]> Reviewed-by: Justin Stitt <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Jens Axboe <[email protected]>
1 parent 3bfeb61 commit 146afeb

File tree

1 file changed

+4
-8
lines changed

1 file changed

+4
-8
lines changed

block/partitions/cmdline.c

Lines changed: 4 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,7 @@ static int parse_subpart(struct cmdline_subpart **subpart, char *partdef)
8181

8282
length = min_t(int, next - partdef,
8383
sizeof(new_subpart->name) - 1);
84-
strncpy(new_subpart->name, partdef, length);
85-
new_subpart->name[length] = '\0';
84+
strscpy(new_subpart->name, partdef, length);
8685

8786
partdef = ++next;
8887
} else
@@ -140,8 +139,7 @@ static int parse_parts(struct cmdline_parts **parts, const char *bdevdef)
140139
}
141140

142141
length = min_t(int, next - bdevdef, sizeof(newparts->name) - 1);
143-
strncpy(newparts->name, bdevdef, length);
144-
newparts->name[length] = '\0';
142+
strscpy(newparts->name, bdevdef, length);
145143
newparts->nr_subparts = 0;
146144

147145
next_subpart = &newparts->subpart;
@@ -153,8 +151,7 @@ static int parse_parts(struct cmdline_parts **parts, const char *bdevdef)
153151
length = (!next) ? (sizeof(buf) - 1) :
154152
min_t(int, next - bdevdef, sizeof(buf) - 1);
155153

156-
strncpy(buf, bdevdef, length);
157-
buf[length] = '\0';
154+
strscpy(buf, bdevdef, length);
158155

159156
ret = parse_subpart(next_subpart, buf);
160157
if (ret)
@@ -267,8 +264,7 @@ static int add_part(int slot, struct cmdline_subpart *subpart,
267264

268265
label_min = min_t(int, sizeof(info->volname) - 1,
269266
sizeof(subpart->name));
270-
strncpy(info->volname, subpart->name, label_min);
271-
info->volname[label_min] = '\0';
267+
strscpy(info->volname, subpart->name, label_min);
272268

273269
snprintf(tmp, sizeof(tmp), "(%s)", info->volname);
274270
strlcat(state->pp_buf, tmp, PAGE_SIZE);

0 commit comments

Comments
 (0)