Skip to content

Commit a06f440

Browse files
committed
tree: coverity fixes
Coverity found some resource leaks and a possible out-of-bounds access in nvme_ctrl_lookup_subsystem_name() where we could overflow the 'path' variable. Signed-off-by: Hannes Reinecke <[email protected]>
1 parent 963dbe8 commit a06f440

1 file changed

Lines changed: 16 additions & 10 deletions

File tree

src/nvme/tree.c

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -578,6 +578,7 @@ static int nvme_scan_subsystem(struct nvme_root *r, const char *name,
578578
nvme_msg(r, LOG_WARNING, "NQN mismatch for subsystem '%s'\n",
579579
name);
580580
s = NULL;
581+
free(subsysnqn);
581582
errno = EINVAL;
582583
return -1;
583584
}
@@ -1092,20 +1093,26 @@ static char *nvme_ctrl_lookup_subsystem_name(nvme_root_t r,
10921093
struct dirent **subsys;
10931094
char *subsys_name = NULL;
10941095
int ret, i;
1095-
char path[PATH_MAX];
10961096

10971097
ret = nvme_scan_subsystems(&subsys);
10981098
if (ret < 0)
10991099
return NULL;
11001100
for (i = 0; i < ret; i++) {
11011101
struct stat st;
1102+
char *path;
11021103

1103-
sprintf(path, "%s/%s/%s", nvme_subsys_sysfs_dir,
1104-
subsys[i]->d_name, ctrl_name);
1104+
if (asprintf(&path, "%s/%s/%s", nvme_subsys_sysfs_dir,
1105+
subsys[i]->d_name, ctrl_name) < 0) {
1106+
errno = ENOMEM;
1107+
return NULL;
1108+
}
11051109
nvme_msg(r, LOG_DEBUG, "lookup subsystem %s\n", path);
1106-
if (stat(path, &st) < 0)
1110+
if (stat(path, &st) < 0) {
1111+
free(path);
11071112
continue;
1113+
}
11081114
subsys_name = strdup(subsys[i]->d_name);
1115+
free(path);
11091116
break;
11101117
}
11111118
nvme_free_dirents(subsys, ret);
@@ -1293,12 +1300,10 @@ static nvme_ctrl_t nvme_ctrl_alloc(nvme_root_t r, nvme_subsystem_t s,
12931300
free(transport);
12941301
if (address)
12951302
free(address);
1296-
if (!c) {
1297-
if (!p) {
1298-
nvme_msg(r, LOG_ERR, "failed to lookup ctrl\n");
1299-
errno = ENODEV;
1300-
} else
1301-
errno = ENOMEM;
1303+
if (!c && !p) {
1304+
nvme_msg(r, LOG_ERR, "failed to lookup ctrl\n");
1305+
errno = ENODEV;
1306+
free(addr);
13021307
return NULL;
13031308
}
13041309
c->address = addr;
@@ -1360,6 +1365,7 @@ nvme_ctrl_t nvme_scan_ctrl(nvme_root_t r, const char *name)
13601365
nvme_msg(r, LOG_ERR,
13611366
"failed to lookup subsystem for controller %s\n",
13621367
name);
1368+
free(subsysnqn);
13631369
free(path);
13641370
errno = ENXIO;
13651371
return NULL;

0 commit comments

Comments
 (0)