Skip to content

Commit e903b27

Browse files
committed
tree: do not try to strdup NULL pointer
blktests nvme/003 using the loop transport fails because nvme_ctrl_alloc tries to strdup NULL pointers (address or sysfs_dir). Introduce a 'safe' strdup version and start this version. Reported-by: Tomáš Bžatek <[email protected]> Reported-by: Yi Zhang <[email protected]> Fixes: e642498 ("tree: free ctrl attributes when (re)configure ctrl") Signed-off-by: Daniel Wagner <[email protected]>
1 parent 6e6154a commit e903b27

2 files changed

Lines changed: 10 additions & 3 deletions

File tree

src/nvme/private.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -335,4 +335,11 @@ void __nvme_mi_mctp_set_ops(const struct __mi_mctp_socket_ops *newops);
335335
int __nvme_import_keys_from_config(nvme_host_t h, nvme_ctrl_t c,
336336
long *keyring_id, long *key_id);
337337

338+
static inline char *xstrdup(const char *s)
339+
{
340+
if (!s)
341+
return NULL;
342+
return strdup(s);
343+
}
344+
338345
#endif /* _LIBNVME_PRIVATE_H */

src/nvme/tree.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2073,8 +2073,8 @@ static int nvme_reconfigure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path,
20732073
}
20742074
closedir(d);
20752075

2076-
c->name = strdup(name);
2077-
c->sysfs_dir = strdup(path);
2076+
c->name = xstrdup(name);
2077+
c->sysfs_dir = xstrdup(path);
20782078
c->firmware = nvme_get_ctrl_attr(c, "firmware_rev");
20792079
c->model = nvme_get_ctrl_attr(c, "model");
20802080
c->state = nvme_get_ctrl_attr(c, "state");
@@ -2230,7 +2230,7 @@ static nvme_ctrl_t nvme_ctrl_alloc(nvme_root_t r, nvme_subsystem_t s,
22302230
return NULL;
22312231
}
22322232
FREE_CTRL_ATTR(c->address);
2233-
c->address = strdup(addr);
2233+
c->address = xstrdup(addr);
22342234
if (s->subsystype && !strcmp(s->subsystype, "discovery"))
22352235
c->discovery_ctrl = true;
22362236
ret = nvme_reconfigure_ctrl(r, c, path, name);

0 commit comments

Comments
 (0)