From 1fdae37b82d0cf16df80f648b5489f7ffd15eae1 Mon Sep 17 00:00:00 2001
From: Li Jinlin <lijinlin3@huawei.com>
Date: Tue, 29 Mar 2022 11:02:38 +0800
Subject: [PATCH 3/6] scsi_get_product_info: fix memleak and avoid to use NULL
 pointer

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@huawei.com>
Signed-off-by: Li Jinlin <lijinlin3@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 94ea176..23ec55a 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

