[libvirt PATCH] tests: Fix fd leaks in virpcivpdtest

Jiri Denemark jdenemar at redhat.com
Thu Feb 10 14:32:57 UTC 2022


Tests testVirPCIVPDReadVPDBytes and testVirPCIVPDParseFullVPDInvalid
failed to properly close open fildescriptors in some cases. Let's fix it
by switching to VIR_AUTOCLOSE in the whole file.

Signed-off-by: Jiri Denemark <jdenemar at redhat.com>
---
 tests/virpcivpdtest.c | 23 ++++++++---------------
 1 file changed, 8 insertions(+), 15 deletions(-)

diff --git a/tests/virpcivpdtest.c b/tests/virpcivpdtest.c
index 3fd40b518c..b4dd68b7aa 100644
--- a/tests/virpcivpdtest.c
+++ b/tests/virpcivpdtest.c
@@ -432,7 +432,7 @@ testPCIVPDGetFieldValueFormat(const void *data G_GNUC_UNUSED)
 static int
 testVirPCIVPDReadVPDBytes(const void *opaque G_GNUC_UNUSED)
 {
-    int fd = -1;
+    VIR_AUTOCLOSE fd = -1;
     g_autofree uint8_t *buf = NULL;
     uint8_t csum = 0;
     size_t readBytes = 0;
@@ -471,7 +471,7 @@ testVirPCIVPDReadVPDBytes(const void *opaque G_GNUC_UNUSED)
 static int
 testVirPCIVPDParseVPDStringResource(const void *opaque G_GNUC_UNUSED)
 {
-    int fd = -1;
+    VIR_AUTOCLOSE fd = -1;
     uint8_t csum = 0;
     size_t dataLen = 0;
     bool result = false;
@@ -488,7 +488,6 @@ testVirPCIVPDParseVPDStringResource(const void *opaque G_GNUC_UNUSED)
         return -1;
 
     result = virPCIVPDParseVPDLargeResourceString(fd, 0, dataLen, &csum, res);
-    VIR_FORCE_CLOSE(fd);
 
     if (!result) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -541,7 +540,7 @@ testVirPCIVPDValidateExampleReadOnlyFields(virPCIVPDResource *res)
 static int
 testVirPCIVPDParseFullVPD(const void *opaque G_GNUC_UNUSED)
 {
-    int fd = -1;
+    VIR_AUTOCLOSE fd = -1;
     size_t dataLen = 0;
 
     g_autoptr(virPCIVPDResource) res = NULL;
@@ -560,7 +559,6 @@ testVirPCIVPDParseFullVPD(const void *opaque G_GNUC_UNUSED)
         return -1;
 
     res = virPCIVPDParse(fd);
-    VIR_FORCE_CLOSE(fd);
 
     if (!res) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -609,7 +607,7 @@ testVirPCIVPDParseFullVPD(const void *opaque G_GNUC_UNUSED)
 static int
 testVirPCIVPDParseZeroLengthRW(const void *opaque G_GNUC_UNUSED)
 {
-    int fd = -1;
+    VIR_AUTOCLOSE fd = -1;
     size_t dataLen = 0;
 
     g_autoptr(virPCIVPDResource) res = NULL;
@@ -630,7 +628,6 @@ testVirPCIVPDParseZeroLengthRW(const void *opaque G_GNUC_UNUSED)
         return -1;
 
     res = virPCIVPDParse(fd);
-    VIR_FORCE_CLOSE(fd);
 
     if (!res) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -662,7 +659,7 @@ testVirPCIVPDParseZeroLengthRW(const void *opaque G_GNUC_UNUSED)
 static int
 testVirPCIVPDParseNoRW(const void *opaque G_GNUC_UNUSED)
 {
-    int fd = -1;
+    VIR_AUTOCLOSE fd = -1;
     size_t dataLen = 0;
 
     g_autoptr(virPCIVPDResource) res = NULL;
@@ -682,7 +679,6 @@ testVirPCIVPDParseNoRW(const void *opaque G_GNUC_UNUSED)
         return -1;
 
     res = virPCIVPDParse(fd);
-    VIR_FORCE_CLOSE(fd);
 
     if (!res) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -714,7 +710,7 @@ testVirPCIVPDParseNoRW(const void *opaque G_GNUC_UNUSED)
 static int
 testVirPCIVPDParseFullVPDSkipInvalidKeywords(const void *opaque G_GNUC_UNUSED)
 {
-    int fd = -1;
+    VIR_AUTOCLOSE fd = -1;
     size_t dataLen = 0;
 
     g_autoptr(virPCIVPDResource) res = NULL;
@@ -737,7 +733,6 @@ testVirPCIVPDParseFullVPDSkipInvalidKeywords(const void *opaque G_GNUC_UNUSED)
         return -1;
 
     res = virPCIVPDParse(fd);
-    VIR_FORCE_CLOSE(fd);
 
     if (!res) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -759,7 +754,7 @@ testVirPCIVPDParseFullVPDSkipInvalidKeywords(const void *opaque G_GNUC_UNUSED)
 static int
 testVirPCIVPDParseFullVPDSkipInvalidValues(const void *opaque G_GNUC_UNUSED)
 {
-    int fd = -1;
+    VIR_AUTOCLOSE fd = -1;
     size_t dataLen = 0;
     size_t i = 0;
     virPCIVPDResourceCustom *custom = NULL;
@@ -792,7 +787,6 @@ testVirPCIVPDParseFullVPDSkipInvalidValues(const void *opaque G_GNUC_UNUSED)
         return -1;
 
     res = virPCIVPDParse(fd);
-    VIR_FORCE_CLOSE(fd);
 
     if (!res) {
         virReportError(VIR_ERR_INTERNAL_ERROR, "%s",
@@ -868,7 +862,6 @@ testVirPCIVPDParseFullVPDSkipInvalidValues(const void *opaque G_GNUC_UNUSED)
 static int
 testVirPCIVPDParseFullVPDInvalid(const void *opaque G_GNUC_UNUSED)
 {
-    int fd = -1;
     size_t dataLen = 0;
 
 # define VPD_INVALID_ZERO_BYTE \
@@ -963,6 +956,7 @@ testVirPCIVPDParseFullVPDInvalid(const void *opaque G_GNUC_UNUSED)
 
 # define TEST_INVALID_VPD(invalidVPD) \
     do { \
+        VIR_AUTOCLOSE fd = -1; \
         g_autoptr(virPCIVPDResource) res = NULL; \
         const uint8_t testCase[] = { invalidVPD }; \
         dataLen = G_N_ELEMENTS(testCase); \
@@ -973,7 +967,6 @@ testVirPCIVPDParseFullVPDInvalid(const void *opaque G_GNUC_UNUSED)
                     "Successfully parsed an invalid VPD - this is not expected"); \
             return -1; \
         } \
-        VIR_FORCE_CLOSE(fd); \
     } while (0);
 
     TEST_INVALID_VPD(VPD_INVALID_ZERO_BYTE);
-- 
2.35.1




More information about the libvir-list mailing list