Skip to content

Commit de8ca5a

Browse files
committed
libnvme: sanitize free() handling
As per the POSIX manual, free() is NULL-safe i.e. we could call it without first checking whether the respective pointer is non-NULL. So apply that here. And while at it, reorder some of the _cleanup_free_ items in the declaration section. Suggested-by: Martin Belanger <martin.belanger@dell.com> Signed-off-by: Martin George <marting@netapp.com>
1 parent 7b7cb76 commit de8ca5a

File tree

3 files changed

+49
-80
lines changed

3 files changed

+49
-80
lines changed

src/nvme/fabrics.c

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1841,10 +1841,8 @@ static char *unescape_uri(const char *str, int len)
18411841
struct nvme_fabrics_uri *nvme_parse_uri(const char *str)
18421842
{
18431843
struct nvme_fabrics_uri *uri;
1844-
_cleanup_free_ char *scheme = NULL;
1845-
_cleanup_free_ char *authority = NULL;
1846-
_cleanup_free_ char *path = NULL;
1847-
_cleanup_free_ char *h = NULL;
1844+
_cleanup_free_ char *scheme = NULL, *authority = NULL;
1845+
_cleanup_free_ char *path = NULL, *h = NULL;
18481846
const char *host;
18491847
int i;
18501848

src/nvme/mi.c

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2533,8 +2533,7 @@ int nvme_mi_aem_disable(nvme_mi_ep_t ep)
25332533

25342534
int rc = aem_disable_enabled(ep);
25352535

2536-
if (ep->aem_ctx)
2537-
free(ep->aem_ctx);
2536+
free(ep->aem_ctx);
25382537
ep->aem_ctx = NULL;
25392538

25402539
return rc;

src/nvme/tree.c

Lines changed: 46 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -130,9 +130,7 @@ int nvme_host_get_ids(nvme_root_t r,
130130
char *hostnqn_arg, char *hostid_arg,
131131
char **hostnqn, char **hostid)
132132
{
133-
_cleanup_free_ char *nqn = NULL;
134-
_cleanup_free_ char *hid = NULL;
135-
_cleanup_free_ char *hnqn = NULL;
133+
_cleanup_free_ char *nqn = NULL, *hid = NULL, *hnqn = NULL;
136134
nvme_host_t h;
137135

138136
/* command line argumments */
@@ -202,8 +200,7 @@ int nvme_host_get_ids(nvme_root_t r,
202200

203201
nvme_host_t nvme_default_host(nvme_root_t r)
204202
{
205-
_cleanup_free_ char *hostnqn = NULL;
206-
_cleanup_free_ char *hostid = NULL;
203+
_cleanup_free_ char *hostnqn = NULL, *hostid = NULL;
207204
struct nvme_host *h;
208205

209206
if (nvme_host_get_ids(r, NULL, NULL, &hostnqn, &hostid))
@@ -421,10 +418,9 @@ const char *nvme_root_get_application(nvme_root_t r)
421418

422419
void nvme_root_set_application(nvme_root_t r, const char *a)
423420
{
424-
if (r->application) {
425-
free(r->application);
426-
r->application = NULL;
427-
}
421+
free(r->application);
422+
r->application = NULL;
423+
428424
if (a)
429425
r->application = strdup(a);
430426
}
@@ -466,10 +462,9 @@ const char *nvme_host_get_hostsymname(nvme_host_t h)
466462

467463
void nvme_host_set_hostsymname(nvme_host_t h, const char *hostsymname)
468464
{
469-
if (h->hostsymname) {
470-
free(h->hostsymname);
471-
h->hostsymname = NULL;
472-
}
465+
free(h->hostsymname);
466+
h->hostsymname = NULL;
467+
473468
if (hostsymname)
474469
h->hostsymname = strdup(hostsymname);
475470
}
@@ -481,10 +476,9 @@ const char *nvme_host_get_dhchap_key(nvme_host_t h)
481476

482477
void nvme_host_set_dhchap_key(nvme_host_t h, const char *key)
483478
{
484-
if (h->dhchap_key) {
485-
free(h->dhchap_key);
486-
h->dhchap_key = NULL;
487-
}
479+
free(h->dhchap_key);
480+
h->dhchap_key = NULL;
481+
488482
if (key)
489483
h->dhchap_key = strdup(key);
490484
}
@@ -528,14 +522,11 @@ void nvme_free_tree(nvme_root_t r)
528522
if (!r)
529523
return;
530524

531-
if (r->options)
532-
free(r->options);
525+
free(r->options);
533526
nvme_for_each_host_safe(r, h, _h)
534527
__nvme_free_host(h);
535-
if (r->config_file)
536-
free(r->config_file);
537-
if (r->application)
538-
free(r->application);
528+
free(r->config_file);
529+
free(r->application);
539530
free(r);
540531
}
541532

@@ -574,10 +565,9 @@ const char *nvme_subsystem_get_application(nvme_subsystem_t s)
574565

575566
void nvme_subsystem_set_application(nvme_subsystem_t s, const char *a)
576567
{
577-
if (s->application) {
578-
free(s->application);
579-
s->application = NULL;
580-
}
568+
free(s->application);
569+
s->application = NULL;
570+
581571
if (a)
582572
s->application = strdup(a);
583573
}
@@ -667,22 +657,15 @@ static void __nvme_free_subsystem(struct nvme_subsystem *s)
667657
nvme_subsystem_for_each_ns_safe(s, n, _n)
668658
__nvme_free_ns(n);
669659

670-
if (s->name)
671-
free(s->name);
660+
free(s->name);
672661
free(s->sysfs_dir);
673662
free(s->subsysnqn);
674-
if (s->model)
675-
free(s->model);
676-
if (s->serial)
677-
free(s->serial);
678-
if (s->firmware)
679-
free(s->firmware);
680-
if (s->subsystype)
681-
free(s->subsystype);
682-
if (s->application)
683-
free(s->application);
684-
if (s->iopolicy)
685-
free(s->iopolicy);
663+
free(s->model);
664+
free(s->serial);
665+
free(s->firmware);
666+
free(s->subsystype);
667+
free(s->application);
668+
free(s->iopolicy);
686669
free(s);
687670
}
688671

@@ -759,10 +742,8 @@ static void __nvme_free_host(struct nvme_host *h)
759742
nvme_for_each_subsystem_safe(h, s, _s)
760743
__nvme_free_subsystem(s);
761744
free(h->hostnqn);
762-
if (h->hostid)
763-
free(h->hostid);
764-
if (h->dhchap_key)
765-
free(h->dhchap_key);
745+
free(h->hostid);
746+
free(h->dhchap_key);
766747
nvme_host_set_hostsymname(h, NULL);
767748
h->r->modified = true;
768749
free(h);
@@ -1131,8 +1112,7 @@ const char *nvme_ctrl_get_state(nvme_ctrl_t c)
11311112
char *state = c->state;
11321113

11331114
c->state = nvme_get_ctrl_attr(c, "state");
1134-
if (state)
1135-
free(state);
1115+
free(state);
11361116
return c->state;
11371117
}
11381118

@@ -1198,10 +1178,9 @@ const char *nvme_ctrl_get_cntlid(nvme_ctrl_t c)
11981178

11991179
void nvme_ctrl_set_dhchap_host_key(nvme_ctrl_t c, const char *key)
12001180
{
1201-
if (c->dhchap_key) {
1202-
free(c->dhchap_key);
1203-
c->dhchap_key = NULL;
1204-
}
1181+
free(c->dhchap_key);
1182+
c->dhchap_key = NULL;
1183+
12051184
if (key)
12061185
c->dhchap_key = strdup(key);
12071186
}
@@ -1213,10 +1192,9 @@ const char *nvme_ctrl_get_dhchap_key(nvme_ctrl_t c)
12131192

12141193
void nvme_ctrl_set_dhchap_key(nvme_ctrl_t c, const char *key)
12151194
{
1216-
if (c->dhchap_ctrl_key) {
1217-
free(c->dhchap_ctrl_key);
1218-
c->dhchap_ctrl_key = NULL;
1219-
}
1195+
free(c->dhchap_ctrl_key);
1196+
c->dhchap_ctrl_key = NULL;
1197+
12201198
if (key)
12211199
c->dhchap_ctrl_key = strdup(key);
12221200
}
@@ -1228,10 +1206,9 @@ const char *nvme_ctrl_get_keyring(nvme_ctrl_t c)
12281206

12291207
void nvme_ctrl_set_keyring(nvme_ctrl_t c, const char *keyring)
12301208
{
1231-
if (c->keyring) {
1232-
free(c->keyring);
1233-
c->keyring = NULL;
1234-
}
1209+
free(c->keyring);
1210+
c->keyring = NULL;
1211+
12351212
if (keyring)
12361213
c->keyring = strdup(keyring);
12371214
}
@@ -1243,10 +1220,9 @@ const char *nvme_ctrl_get_tls_key_identity(nvme_ctrl_t c)
12431220

12441221
void nvme_ctrl_set_tls_key_identity(nvme_ctrl_t c, const char *identity)
12451222
{
1246-
if (c->tls_key_identity) {
1247-
free(c->tls_key_identity);
1248-
c->tls_key_identity = NULL;
1249-
}
1223+
free(c->tls_key_identity);
1224+
c->tls_key_identity = NULL;
1225+
12501226
if (identity)
12511227
c->tls_key_identity = strdup(identity);
12521228
}
@@ -1258,10 +1234,9 @@ const char *nvme_ctrl_get_tls_key(nvme_ctrl_t c)
12581234

12591235
void nvme_ctrl_set_tls_key(nvme_ctrl_t c, const char *key)
12601236
{
1261-
if (c->tls_key) {
1262-
free(c->tls_key);
1263-
c->tls_key = NULL;
1264-
}
1237+
free(c->tls_key);
1238+
c->tls_key = NULL;
1239+
12651240
if (key)
12661241
c->tls_key = strdup(key);
12671242
}
@@ -1332,7 +1307,7 @@ nvme_path_t nvme_ctrl_next_path(nvme_ctrl_t c, nvme_path_t p)
13321307
}
13331308

13341309
#define FREE_CTRL_ATTR(a) \
1335-
do { if (a) { free(a); (a) = NULL; } } while (0)
1310+
do { free(a); (a) = NULL; } while (0)
13361311
void nvme_deconfigure_ctrl(nvme_ctrl_t c)
13371312
{
13381313
nvme_ctrl_release_fd(c);
@@ -1980,8 +1955,7 @@ static char *nvme_ctrl_lookup_phy_slot(nvme_root_t r, const char *address)
19801955
if (entry->d_type == DT_DIR &&
19811956
strncmp(entry->d_name, ".", 1) != 0 &&
19821957
strncmp(entry->d_name, "..", 2) != 0) {
1983-
_cleanup_free_ char *path = NULL;
1984-
_cleanup_free_ char *addr = NULL;
1958+
_cleanup_free_ char *path = NULL, *addr = NULL;
19851959

19861960
ret = asprintf(&path, "%s/%s",
19871961
slots_sysfs_dir, entry->d_name);
@@ -2282,8 +2256,7 @@ nvme_ctrl_t nvme_scan_ctrl(nvme_root_t r, const char *name)
22822256
hostid = nvme_get_attr(path, "hostid");
22832257
h = nvme_lookup_host(r, hostnqn, hostid);
22842258
if (h) {
2285-
if (h->dhchap_key)
2286-
free(h->dhchap_key);
2259+
free(h->dhchap_key);
22872260
h->dhchap_key = nvme_get_attr(path, "dhchap_secret");
22882261
if (h->dhchap_key && !strcmp(h->dhchap_key, "none")) {
22892262
free(h->dhchap_key);
@@ -2914,9 +2887,8 @@ static char *nvme_ns_generic_to_blkdev(const char *generic)
29142887
static struct nvme_ns *__nvme_scan_namespace(const char *sysfs_dir, const char *name)
29152888
{
29162889
struct nvme_ns *n;
2917-
_cleanup_free_ char *path = NULL;
2890+
_cleanup_free_ char *path = NULL, *blkdev = NULL;
29182891
int ret;
2919-
_cleanup_free_ char *blkdev = NULL;
29202892

29212893
blkdev = nvme_ns_generic_to_blkdev(name);
29222894
if (!blkdev) {

0 commit comments

Comments
 (0)