From e903b27fde86c442e10bf510627f24a92e6e31eb Mon Sep 17 00:00:00 2001 From: Daniel Wagner Date: Thu, 31 Jul 2025 11:24:15 +0200 Subject: [PATCH] tree: do not try to strdup NULL pointer MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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 Reported-by: Yi Zhang Fixes: e64249888521 ("tree: free ctrl attributes when (re)configure ctrl") Signed-off-by: Daniel Wagner --- src/nvme/private.h | 7 +++++++ src/nvme/tree.c | 6 +++--- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/nvme/private.h b/src/nvme/private.h index ac5949678..f2ba299b6 100644 --- a/src/nvme/private.h +++ b/src/nvme/private.h @@ -335,4 +335,11 @@ void __nvme_mi_mctp_set_ops(const struct __mi_mctp_socket_ops *newops); int __nvme_import_keys_from_config(nvme_host_t h, nvme_ctrl_t c, long *keyring_id, long *key_id); +static inline char *xstrdup(const char *s) +{ + if (!s) + return NULL; + return strdup(s); +} + #endif /* _LIBNVME_PRIVATE_H */ diff --git a/src/nvme/tree.c b/src/nvme/tree.c index 9aaaa1b50..d79707439 100644 --- a/src/nvme/tree.c +++ b/src/nvme/tree.c @@ -2073,8 +2073,8 @@ static int nvme_reconfigure_ctrl(nvme_root_t r, nvme_ctrl_t c, const char *path, } closedir(d); - c->name = strdup(name); - c->sysfs_dir = strdup(path); + c->name = xstrdup(name); + c->sysfs_dir = xstrdup(path); c->firmware = nvme_get_ctrl_attr(c, "firmware_rev"); c->model = nvme_get_ctrl_attr(c, "model"); 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, return NULL; } FREE_CTRL_ATTR(c->address); - c->address = strdup(addr); + c->address = xstrdup(addr); if (s->subsystype && !strcmp(s->subsystype, "discovery")) c->discovery_ctrl = true; ret = nvme_reconfigure_ctrl(r, c, path, name);