[PATCH 3/5] nvdisk.c: Fix Resource leak (CID #1439297)

Ryan Long ryan.long at oarcorp.com
Fri Mar 12 15:18:44 UTC 2021


CID 1439297: Resource leak in rtems_nvdisk_initialize().

Closes #4298
---
 cpukit/libblock/src/nvdisk.c | 8 +++++++-
 1 file changed, 7 insertions(+), 1 deletion(-)

diff --git a/cpukit/libblock/src/nvdisk.c b/cpukit/libblock/src/nvdisk.c
index a7f4167..d742baf 100644
--- a/cpukit/libblock/src/nvdisk.c
+++ b/cpukit/libblock/src/nvdisk.c
@@ -766,8 +766,10 @@ rtems_nvdisk_initialize (rtems_device_major_number major RTEMS_UNUSED,
     nvd->info_level   = c->info_level;
 
     nvd->devices = calloc (c->device_count, sizeof (rtems_nvdisk_device_ctl));
-    if (!nvd->devices)
+    if (!nvd->devices) {
+      free(nvd);
       return RTEMS_NO_MEMORY;
+    }
 
     for (device = 0; device < c->device_count; device++)
     {
@@ -790,6 +792,8 @@ rtems_nvdisk_initialize (rtems_device_major_number major RTEMS_UNUSED,
                              rtems_nvdisk_ioctl, nvd);
     if (sc != RTEMS_SUCCESSFUL)
     {
+      free(nvd->devices);
+      free(nvd);
       rtems_nvdisk_error ("disk create phy failed");
       return sc;
     }
@@ -797,5 +801,7 @@ rtems_nvdisk_initialize (rtems_device_major_number major RTEMS_UNUSED,
     rtems_mutex_init (&nvd->lock, "NV Disk");
   }
 
+  free(nvd->devices);
+  free(nvd);
   return RTEMS_SUCCESSFUL;
 }
-- 
1.8.3.1



More information about the devel mailing list