[parted-devel] [PATCH 3/6] scsi_get_product_info: fix memleak and avoid to use NULL pointer

Wu Guanghao wuguanghao3 at huawei.com
Wed Dec 15 08:30:01 GMT 2021


Need to ensure that *vendor and *product are not NULL in scsi_query_product_info()
before return 1, otherwise a null pointer may be used.

Regardless of whether scsi_query_product_info() returns success or failed,
vendor and product should be released, otherwise there will be a memory leak

Signed-off-by: Wu Guanghao <wuguanghao3 at huawei.com>
---
 libparted/arch/linux.c | 9 +++++----
 1 file changed, 5 insertions(+), 4 deletions(-)

diff --git a/libparted/arch/linux.c b/libparted/arch/linux.c
index 430d02e..095dc9f 100644
--- a/libparted/arch/linux.c
+++ b/libparted/arch/linux.c
@@ -1130,7 +1130,9 @@ scsi_query_product_info (PedDevice* dev, char **vendor, char **product)
         buf[16] = '\0';
         *product = strip_name (buf);
 
-        return 1;
+        if (*vendor && *product)
+            return 1;
+        return 0;
 }
 
 /* This function provides the vendor and product name for a SCSI device.
@@ -1144,7 +1146,6 @@ scsi_get_product_info (PedDevice* dev, char **vendor, char **product)
         *product = read_device_sysfs_file (dev, "model");
         if (*vendor && *product)
                 return 1;
-
         return scsi_query_product_info (dev, vendor, product);
 }
 
@@ -1188,11 +1189,11 @@ init_scsi (PedDevice* dev)
 
         if (scsi_get_product_info (dev, &vendor, &product)) {
                 sprintf (dev->model, "%.8s %.16s", vendor, product);
-                free (vendor);
-                free (product);
         } else {
                 strcpy (dev->model, "Generic SCSI");
         }
+        free (vendor);
+        free (product);
 
         if (!_device_probe_geometry (dev))
                 goto error_close_dev;
-- 
2.27.0




More information about the parted-devel mailing list