nutanix / libvfio-user

framework for emulating devices in userspace
BSD 3-Clause "New" or "Revised" License
162 stars 51 forks source link

test/unit-tests.c GCC13 compilation error, dangling-pointer #734

Closed kgodzwox closed 1 year ago

kgodzwox commented 1 year ago

Hi. A compilation error occurs on Fedora 38 with GCC13 (gcc 13.1.1 "cc (GCC) 13.1.1 20230511 (Red Hat 13.1.1-2)") for test/unit-tests.c file.

../../../libvfio-user/test/unit-tests.c: In function ‘test_device_is_stopped_and_copying’:
  ../../../libvfio-user/test/unit-tests.c:585:23: error: storing the address of local variable ‘migration’ in ‘vfu_ctx.migration’ [-Werror=dangling-pointer=]
    585 |     vfu_ctx.migration = &migration;
        |     ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~
  ../../../libvfio-user/test/unit-tests.c:584:22: note: ‘migration’ declared here
    584 |     struct migration migration;
        |                      ^~~~~~~~~
  ../../../libvfio-user/test/unit-tests.c:62:18: note: ‘vfu_ctx’ declared here
     62 | static vfu_ctx_t vfu_ctx;
        |                  ^~~~~~~
jimharris commented 1 year ago

I think it needs to clear vfu_ctx.migration back to NULL before returning, since vfu_ctx is a global variable.

@kgodzwox, can you confirm this patch fixes the issue? If so I'll create a pull request for it.

diff --git a/test/unit-tests.c b/test/unit-tests.c
index cdb88a2..81e53ca 100644
--- a/test/unit-tests.c
+++ b/test/unit-tests.c
@@ -601,6 +601,7 @@ test_device_is_stopped_and_copying(UNUSED void **state)
             assert_false(r);
         }
     }
+    vfu_ctx.migration = NULL;
 }

 static void
tmakatos commented 1 year ago

@jimharris I confirmed your patch fixes compilation.

kgodzwox commented 1 year ago

I also confirm this

jimharris commented 1 year ago

https://github.com/nutanix/libvfio-user/pull/735

jimharris commented 1 year ago

The pull request was merged, this issue can be closed. Thanks!