From b91350117398a0d88da738c9d625b6a5dbd4f1ea Mon Sep 17 00:00:00 2001 From: Jes Sorensen Date: Mon, 7 Mar 2016 12:33:49 -0500 Subject: [PATCH] {platform,super}-intel: Fix two resource leaks The code did not free 'dir' allocated by opendir(). An additional benefit is that this simplifies the for() loops. Fixes: 60f0f54d ("IMSM: Add support for VMD") Reviewed-by: NeilBrown Signed-off-by: Jes Sorensen --- platform-intel.c | 7 ++++++- super-intel.c | 6 +++++- 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/platform-intel.c b/platform-intel.c index 88818f3..c60fd9e 100644 --- a/platform-intel.c +++ b/platform-intel.c @@ -724,8 +724,10 @@ char *vmd_domain_to_controller(struct sys_dev *hba, char *buf) return NULL; dir = opendir("/sys/bus/pci/drivers/vmd"); + if (!dir) + return NULL; - for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) { + for (ent = readdir(dir); ent; ent = readdir(dir)) { sprintf(path, "/sys/bus/pci/drivers/vmd/%s/domain/device", ent->d_name); @@ -734,8 +736,11 @@ char *vmd_domain_to_controller(struct sys_dev *hba, char *buf) if (strncmp(buf, hba->path, strlen(buf)) == 0) { sprintf(path, "/sys/bus/pci/drivers/vmd/%s", ent->d_name); + closedir(dir); return realpath(path, buf); } } + + closedir(dir); return NULL; } diff --git a/super-intel.c b/super-intel.c index 158f4e8..e1bee75 100644 --- a/super-intel.c +++ b/super-intel.c @@ -1781,7 +1781,10 @@ static int print_vmd_attached_devs(struct sys_dev *hba) * this hba */ dir = opendir("/sys/bus/pci/drivers/nvme"); - for (ent = dir ? readdir(dir) : NULL; ent; ent = readdir(dir)) { + if (!dir) + return 1; + + for (ent = readdir(dir); ent; ent = readdir(dir)) { int n; /* is 'ent' a device? check that the 'subsystem' link exists and @@ -1814,6 +1817,7 @@ static int print_vmd_attached_devs(struct sys_dev *hba) free(rp); } + closedir(dir); return 0; }