Blame SOURCES/1000-Downstream-fix-covscan-issue-close-fd-called-twice.patch

311d97
From 5466375683a48bd650091781991d9e4facd06ba6 Mon Sep 17 00:00:00 2001
311d97
From: rpm-build <rpm-build>
311d97
Date: Fri, 26 Jun 2020 21:35:46 +0300
311d97
Subject: [PATCH] Downstream: fix covscan issue: close(fd) called twice
311d97
311d97
Seems it's already fixed upstream, but by several commits, that
311d97
change more things.
311d97
311d97
This simple patch, just prevents the case of calling close(fd) twice
311d97
311d97
Warning is:
311d97
Error: USE_AFTER_FREE (CWE-416): [#def2]
311d97
libusb-1.0.23/libusb/os/linux_usbfs.c:1043: closed_arg: "close(int)" closes "fd".
311d97
libusb-1.0.23/libusb/os/linux_usbfs.c:1054: double_close: Calling "close(int)"
311d97
closes handle "fd" which has already been closed.
311d97
---
311d97
 libusb/os/linux_usbfs.c | 6 ++++--
311d97
 1 file changed, 4 insertions(+), 2 deletions(-)
311d97
311d97
diff --git a/libusb/os/linux_usbfs.c b/libusb/os/linux_usbfs.c
311d97
index 4179b9a..537f0dd 100644
311d97
--- a/libusb/os/linux_usbfs.c
311d97
+++ b/libusb/os/linux_usbfs.c
311d97
@@ -1039,8 +1039,10 @@ static int initialize_device(struct libusb_device *dev, uint8_t busnum,
311d97
 		priv->descriptors_len += r;
311d97
 	} while (priv->descriptors_len == descriptors_size);
311d97
 
311d97
-	if (fd != wrapped_fd)
311d97
+	if (fd != wrapped_fd) {
311d97
 		close(fd);
311d97
+		fd = -1;
311d97
+	}
311d97
 
311d97
 	if (priv->descriptors_len < DEVICE_DESC_LENGTH) {
311d97
 		usbi_err(ctx, "short descriptor read (%d)",
311d97
@@ -1050,7 +1052,7 @@ static int initialize_device(struct libusb_device *dev, uint8_t busnum,
311d97
 
311d97
 	if (sysfs_dir && sysfs_can_relate_devices)
311d97
 	{
311d97
-		if (fd != wrapped_fd)
311d97
+		if ((fd >= 0) && (fd != wrapped_fd))
311d97
 			close(fd);
311d97
 		return LIBUSB_SUCCESS;
311d97
 	}
311d97
-- 
311d97
2.26.2
311d97