Skip to content

Commit 5281883

Browse files
martin-gpyigaw
authored andcommitted
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. Suggested-by: Martin Belanger <martin.belanger@dell.com> Signed-off-by: Martin George <marting@netapp.com>
1 parent 7b7cb76 commit 5281883

File tree

2 files changed

+43
-67
lines changed

2 files changed

+43
-67
lines changed

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: 42 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -421,10 +421,9 @@ const char *nvme_root_get_application(nvme_root_t r)
421421

422422
void nvme_root_set_application(nvme_root_t r, const char *a)
423423
{
424-
if (r->application) {
425-
free(r->application);
426-
r->application = NULL;
427-
}
424+
free(r->application);
425+
r->application = NULL;
426+
428427
if (a)
429428
r->application = strdup(a);
430429
}
@@ -466,10 +465,9 @@ const char *nvme_host_get_hostsymname(nvme_host_t h)
466465

467466
void nvme_host_set_hostsymname(nvme_host_t h, const char *hostsymname)
468467
{
469-
if (h->hostsymname) {
470-
free(h->hostsymname);
471-
h->hostsymname = NULL;
472-
}
468+
free(h->hostsymname);
469+
h->hostsymname = NULL;
470+
473471
if (hostsymname)
474472
h->hostsymname = strdup(hostsymname);
475473
}
@@ -481,10 +479,9 @@ const char *nvme_host_get_dhchap_key(nvme_host_t h)
481479

482480
void nvme_host_set_dhchap_key(nvme_host_t h, const char *key)
483481
{
484-
if (h->dhchap_key) {
485-
free(h->dhchap_key);
486-
h->dhchap_key = NULL;
487-
}
482+
free(h->dhchap_key);
483+
h->dhchap_key = NULL;
484+
488485
if (key)
489486
h->dhchap_key = strdup(key);
490487
}
@@ -528,14 +525,11 @@ void nvme_free_tree(nvme_root_t r)
528525
if (!r)
529526
return;
530527

531-
if (r->options)
532-
free(r->options);
528+
free(r->options);
533529
nvme_for_each_host_safe(r, h, _h)
534530
__nvme_free_host(h);
535-
if (r->config_file)
536-
free(r->config_file);
537-
if (r->application)
538-
free(r->application);
531+
free(r->config_file);
532+
free(r->application);
539533
free(r);
540534
}
541535

@@ -574,10 +568,9 @@ const char *nvme_subsystem_get_application(nvme_subsystem_t s)
574568

575569
void nvme_subsystem_set_application(nvme_subsystem_t s, const char *a)
576570
{
577-
if (s->application) {
578-
free(s->application);
579-
s->application = NULL;
580-
}
571+
free(s->application);
572+
s->application = NULL;
573+
581574
if (a)
582575
s->application = strdup(a);
583576
}
@@ -667,22 +660,15 @@ static void __nvme_free_subsystem(struct nvme_subsystem *s)
667660
nvme_subsystem_for_each_ns_safe(s, n, _n)
668661
__nvme_free_ns(n);
669662

670-
if (s->name)
671-
free(s->name);
663+
free(s->name);
672664
free(s->sysfs_dir);
673665
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);
666+
free(s->model);
667+
free(s->serial);
668+
free(s->firmware);
669+
free(s->subsystype);
670+
free(s->application);
671+
free(s->iopolicy);
686672
free(s);
687673
}
688674

@@ -759,10 +745,8 @@ static void __nvme_free_host(struct nvme_host *h)
759745
nvme_for_each_subsystem_safe(h, s, _s)
760746
__nvme_free_subsystem(s);
761747
free(h->hostnqn);
762-
if (h->hostid)
763-
free(h->hostid);
764-
if (h->dhchap_key)
765-
free(h->dhchap_key);
748+
free(h->hostid);
749+
free(h->dhchap_key);
766750
nvme_host_set_hostsymname(h, NULL);
767751
h->r->modified = true;
768752
free(h);
@@ -1131,8 +1115,7 @@ const char *nvme_ctrl_get_state(nvme_ctrl_t c)
11311115
char *state = c->state;
11321116

11331117
c->state = nvme_get_ctrl_attr(c, "state");
1134-
if (state)
1135-
free(state);
1118+
free(state);
11361119
return c->state;
11371120
}
11381121

@@ -1198,10 +1181,9 @@ const char *nvme_ctrl_get_cntlid(nvme_ctrl_t c)
11981181

11991182
void nvme_ctrl_set_dhchap_host_key(nvme_ctrl_t c, const char *key)
12001183
{
1201-
if (c->dhchap_key) {
1202-
free(c->dhchap_key);
1203-
c->dhchap_key = NULL;
1204-
}
1184+
free(c->dhchap_key);
1185+
c->dhchap_key = NULL;
1186+
12051187
if (key)
12061188
c->dhchap_key = strdup(key);
12071189
}
@@ -1213,10 +1195,9 @@ const char *nvme_ctrl_get_dhchap_key(nvme_ctrl_t c)
12131195

12141196
void nvme_ctrl_set_dhchap_key(nvme_ctrl_t c, const char *key)
12151197
{
1216-
if (c->dhchap_ctrl_key) {
1217-
free(c->dhchap_ctrl_key);
1218-
c->dhchap_ctrl_key = NULL;
1219-
}
1198+
free(c->dhchap_ctrl_key);
1199+
c->dhchap_ctrl_key = NULL;
1200+
12201201
if (key)
12211202
c->dhchap_ctrl_key = strdup(key);
12221203
}
@@ -1228,10 +1209,9 @@ const char *nvme_ctrl_get_keyring(nvme_ctrl_t c)
12281209

12291210
void nvme_ctrl_set_keyring(nvme_ctrl_t c, const char *keyring)
12301211
{
1231-
if (c->keyring) {
1232-
free(c->keyring);
1233-
c->keyring = NULL;
1234-
}
1212+
free(c->keyring);
1213+
c->keyring = NULL;
1214+
12351215
if (keyring)
12361216
c->keyring = strdup(keyring);
12371217
}
@@ -1243,10 +1223,9 @@ const char *nvme_ctrl_get_tls_key_identity(nvme_ctrl_t c)
12431223

12441224
void nvme_ctrl_set_tls_key_identity(nvme_ctrl_t c, const char *identity)
12451225
{
1246-
if (c->tls_key_identity) {
1247-
free(c->tls_key_identity);
1248-
c->tls_key_identity = NULL;
1249-
}
1226+
free(c->tls_key_identity);
1227+
c->tls_key_identity = NULL;
1228+
12501229
if (identity)
12511230
c->tls_key_identity = strdup(identity);
12521231
}
@@ -1258,10 +1237,9 @@ const char *nvme_ctrl_get_tls_key(nvme_ctrl_t c)
12581237

12591238
void nvme_ctrl_set_tls_key(nvme_ctrl_t c, const char *key)
12601239
{
1261-
if (c->tls_key) {
1262-
free(c->tls_key);
1263-
c->tls_key = NULL;
1264-
}
1240+
free(c->tls_key);
1241+
c->tls_key = NULL;
1242+
12651243
if (key)
12661244
c->tls_key = strdup(key);
12671245
}
@@ -1332,7 +1310,7 @@ nvme_path_t nvme_ctrl_next_path(nvme_ctrl_t c, nvme_path_t p)
13321310
}
13331311

13341312
#define FREE_CTRL_ATTR(a) \
1335-
do { if (a) { free(a); (a) = NULL; } } while (0)
1313+
do { free(a); (a) = NULL; } while (0)
13361314
void nvme_deconfigure_ctrl(nvme_ctrl_t c)
13371315
{
13381316
nvme_ctrl_release_fd(c);
@@ -2282,8 +2260,7 @@ nvme_ctrl_t nvme_scan_ctrl(nvme_root_t r, const char *name)
22822260
hostid = nvme_get_attr(path, "hostid");
22832261
h = nvme_lookup_host(r, hostnqn, hostid);
22842262
if (h) {
2285-
if (h->dhchap_key)
2286-
free(h->dhchap_key);
2263+
free(h->dhchap_key);
22872264
h->dhchap_key = nvme_get_attr(path, "dhchap_secret");
22882265
if (h->dhchap_key && !strcmp(h->dhchap_key, "none")) {
22892266
free(h->dhchap_key);

0 commit comments

Comments
 (0)