Skip to content

Commit 648114f

Browse files
calebsanderigaw
authored andcommitted
nvme: use argconfig_parse_comma_sep_array_u16() in attach-ns
nvme_attach_ns() is using argconfig_parse_comma_sep_array() to parse a list of ints and then copying them into a list of u16s. Use argconfig_parse_comma_sep_array_u16() instead to save on memory and avoid needing to copy the values to another list. This also checks that the values fit in u16. Also use NVME_ID_CTRL_LIST_MAX instead of hard-coding the max list size. Signed-off-by: Caleb Sander Mateos <csander@purestorage.com>
1 parent 72f3ab6 commit 648114f

File tree

1 file changed

+5
-11
lines changed

1 file changed

+5
-11
lines changed

nvme.c

+5-11
Original file line numberDiff line numberDiff line change
@@ -2804,9 +2804,9 @@ static int delete_ns(int argc, char **argv, struct command *cmd, struct plugin *
28042804
static int nvme_attach_ns(int argc, char **argv, int attach, const char *desc, struct command *cmd)
28052805
{
28062806
_cleanup_free_ struct nvme_ctrl_list *cntlist = NULL;
2807-
_cleanup_free_ __u16 *ctrlist = NULL;
28082807
_cleanup_nvme_dev_ struct nvme_dev *dev = NULL;
2809-
int err, num, i, list[2048];
2808+
int err, num;
2809+
__u16 list[NVME_ID_CTRL_LIST_MAX];
28102810

28112811
const char *namespace_id = "namespace to attach";
28122812
const char *cont = "optional comma-sep controller id list";
@@ -2834,7 +2834,8 @@ static int nvme_attach_ns(int argc, char **argv, int attach, const char *desc, s
28342834
return -EINVAL;
28352835
}
28362836

2837-
num = argconfig_parse_comma_sep_array(cfg.cntlist, list, 2047);
2837+
num = argconfig_parse_comma_sep_array_u16(cfg.cntlist,
2838+
list, ARRAY_SIZE(list));
28382839
if (!num)
28392840
fprintf(stderr, "warning: empty controller-id list will result in no actual change in namespace attachment\n");
28402841

@@ -2847,14 +2848,7 @@ static int nvme_attach_ns(int argc, char **argv, int attach, const char *desc, s
28472848
if (!cntlist)
28482849
return -ENOMEM;
28492850

2850-
ctrlist = nvme_alloc(sizeof(*ctrlist) * 2048);
2851-
if (!ctrlist)
2852-
return -ENOMEM;
2853-
2854-
for (i = 0; i < num; i++)
2855-
ctrlist[i] = (__u16)list[i];
2856-
2857-
nvme_init_ctrl_list(cntlist, num, ctrlist);
2851+
nvme_init_ctrl_list(cntlist, num, list);
28582852

28592853
if (attach)
28602854
err = nvme_cli_ns_attach_ctrls(dev, cfg.namespace_id,

0 commit comments

Comments
 (0)