Skip to content

Commit a616ace

Browse files
committed
libnvme: make free functions null-pointer safe
Currently, some free functions check for null pointers, while others do not. Update all free functions to be null-pointer safe. Signed-off-by: Daniel Wagner <[email protected]>
1 parent 0a2eb77 commit a616ace

3 files changed

Lines changed: 28 additions & 5 deletions

File tree

libnvme/src/nvme/fabrics.c

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ const char *nvmf_dev = "/dev/nvme-fabrics";
4444

4545
static inline void free_uri(struct libnvmf_uri **uri)
4646
{
47-
if (*uri)
48-
libnvmf_uri_free(*uri);
47+
libnvmf_uri_free(*uri);
4948
}
5049
#define __cleanup_uri __cleanup(free_uri)
5150

@@ -1517,6 +1516,9 @@ __public int libnvmf_discovery_args_create(struct libnvmf_discovery_args **argsp
15171516

15181517
__public void libnvmf_discovery_args_free(struct libnvmf_discovery_args *args)
15191518
{
1519+
if (!args)
1520+
return;
1521+
15201522
free(args);
15211523
}
15221524

@@ -2642,6 +2644,9 @@ __public int libnvmf_nbft_read_files(struct libnvme_global_ctx *ctx, char *path,
26422644

26432645
__public void libnvmf_nbft_free(struct libnvme_global_ctx *ctx, struct nbft_file_entry *head)
26442646
{
2647+
if (!head)
2648+
return;
2649+
26452650
while (head) {
26462651
struct nbft_file_entry *next = head->next;
26472652

libnvme/src/nvme/tree.c

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -508,6 +508,9 @@ static void __nvme_free_ns(struct libnvme_ns *n)
508508
/* Stub for SWIG */
509509
__public void libnvme_free_ns(struct libnvme_ns *n)
510510
{
511+
if (!n)
512+
return;
513+
511514
__nvme_free_ns(n);
512515
}
513516

@@ -552,6 +555,10 @@ __public void libnvme_subsystem_release_fds(struct libnvme_subsystem *s)
552555
*/
553556
__public void libnvme_free_subsystem(libnvme_subsystem_t s)
554557
{
558+
if (!s)
559+
return;
560+
561+
__nvme_free_subsystem(s);
555562
}
556563

557564
struct libnvme_subsystem *nvme_alloc_subsystem(struct libnvme_host *h,
@@ -637,6 +644,9 @@ __public void libnvme_host_release_fds(struct libnvme_host *h)
637644
/* Stub for SWIG */
638645
__public void libnvme_free_host(struct libnvme_host *h)
639646
{
647+
if (!h)
648+
return;
649+
640650
__libnvme_free_host(h);
641651
}
642652

@@ -831,6 +841,9 @@ __public int libnvme_path_get_queue_depth(libnvme_path_t p)
831841

832842
void nvme_free_path(struct libnvme_path *p)
833843
{
844+
if (!p)
845+
return;
846+
834847
list_del_init(&p->entry);
835848
list_del_init(&p->nentry);
836849
free(p->name);
@@ -1043,6 +1056,9 @@ static void __libnvme_free_ctrl(libnvme_ctrl_t c)
10431056

10441057
__public void libnvme_free_ctrl(libnvme_ctrl_t c)
10451058
{
1059+
if (!c)
1060+
return;
1061+
10461062
__libnvme_free_ctrl(c);
10471063
}
10481064

util/cleanup.h

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -42,14 +42,16 @@ static inline void cleanup_nvme_global_ctx(struct libnvme_global_ctx **ctx)
4242
}
4343
#define __cleanup_nvme_global_ctx __cleanup(cleanup_nvme_global_ctx)
4444

45-
static inline DEFINE_CLEANUP_FUNC(cleanup_nvme_ctrl, libnvme_ctrl_t, libnvme_free_ctrl)
45+
static inline void cleanup_nvme_ctrl(libnvme_ctrl_t *__p)
46+
{
47+
libnvme_free_ctrl(*__p);
48+
}
4649
#define __cleanup_nvme_ctrl __cleanup(cleanup_nvme_ctrl)
4750

4851
#ifdef CONFIG_FABRICS
4952
static inline void free_uri(struct libnvmf_uri **uri)
5053
{
51-
if (*uri)
52-
libnvmf_uri_free(*uri);
54+
libnvmf_uri_free(*uri);
5355
}
5456
#define __cleanup_uri __cleanup(free_uri)
5557

0 commit comments

Comments
 (0)