[libvirt PATCH] tests: avoid referencing stale readdir pointer

Daniel P. Berrangé posted 1 patch 2 weeks ago
Test syntax-check failed
Patches applied successfully (tree, apply log)
git fetch https://github.com/patchew-project/libvirt tags/patchew/20200207145836.2783142-1-berrange@redhat.com
tests/testutilsqemu.c | 5 +++--
1 file changed, 3 insertions(+), 2 deletions(-)

[libvirt PATCH] tests: avoid referencing stale readdir pointer

Posted by Daniel P. Berrangé 2 weeks ago
The contents of 'struct dirent' are only valid until the next call to
readdir() or closedir(). It is thus invalid to save a pointer to the
'd_name' field. Somehow this hasn't affected the test suite until
recently when FreeBSD 12 started showing use of uninitialized memory
resulting in test failures.

Signed-off-by: Daniel P. Berrangé <berrange@redhat.com>
---

Pushed as a build fix

 tests/testutilsqemu.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

diff --git a/tests/testutilsqemu.c b/tests/testutilsqemu.c
index c17e284930..0cb9a7456d 100644
--- a/tests/testutilsqemu.c
+++ b/tests/testutilsqemu.c
@@ -497,7 +497,7 @@ testQemuGetLatestCapsForArch(const char *arch,
     char *tmp = NULL;
     unsigned long maxver = 0;
     unsigned long ver;
-    const char *maxname = NULL;
+    g_autofree char *maxname = NULL;
     char *ret = NULL;
 
     fullsuffix = g_strdup_printf("%s.%s", arch, suffix);
@@ -522,7 +522,8 @@ testQemuGetLatestCapsForArch(const char *arch,
         }
 
         if (ver > maxver) {
-            maxname = ent->d_name;
+            g_free(maxname);
+            maxname = g_strdup(ent->d_name);
             maxver = ver;
         }
     }
-- 
2.24.1