]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
tests: Fix fd leaks in virpcivpdtest
authorJiri Denemark <jdenemar@redhat.com>
Thu, 10 Feb 2022 10:32:00 +0000 (11:32 +0100)
committerJiri Denemark <jdenemar@redhat.com>
Fri, 11 Feb 2022 14:06:19 +0000 (15:06 +0100)
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@redhat.com>
Reviewed-by: Michal Privoznik <mprivozn@redhat.com>
tests/virpcivpdtest.c

index 3fd40b518c114c218ea02c66f92c894d25ab63b1..b4dd68b7aa8203609e0aabfc1d22ff78d2817e4d 100644 (file)
@@ -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);