]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
iommufd/selftest: Fix build warnings due to uninitialized mfd
authorNicolin Chen <nicolinc@nvidia.com>
Tue, 24 Jun 2025 18:00:48 +0000 (11:00 -0700)
committerJason Gunthorpe <jgg@nvidia.com>
Tue, 24 Jun 2025 18:45:13 +0000 (15:45 -0300)
Commit 869c788909b9 ("selftests: harness: Stop using setjmp()/longjmp()")
changed the harness structure. For some unknown reason, two build warnings
occur to the iommufd selftest:

iommufd.c: In function ‘wrapper_iommufd_mock_domain_all_aligns’:
iommufd.c:1807:17: warning: ‘mfd’ may be used uninitialized in this function
 1807 |                 close(mfd);
      |                 ^~~~~~~~~~
iommufd.c:1767:13: note: ‘mfd’ was declared here
 1767 |         int mfd;
      |             ^~~
iommufd.c: In function ‘wrapper_iommufd_mock_domain_all_aligns_copy’:
iommufd.c:1870:17: warning: ‘mfd’ may be used uninitialized in this function
 1870 |                 close(mfd);
      |                 ^~~~~~~~~~
iommufd.c:1819:13: note: ‘mfd’ was declared here
 1819 |         int mfd;
      |             ^~~

All the mfd have been used in the variant->file path only, so it's likely
a false alarm.

FWIW, the commit mentioned above does not cause this, yet it might affect
gcc in a certain way that resulted in the warnings. It is also found that
ading a dummy setjmp (which doesn't make sense) could mute the warnings:
https://lore.kernel.org/all/aEi8DV+ReF3v3Rlf@nvidia.com/

The job of this selftest is to catch kernel bug, while such warnings will
unlikely disrupt its role. Mute the warning by force initializing the mfd
and add an ASSERT_GT().

Link: https://patch.msgid.link/r/6951d85d5cd34cbf22abab7714542654e63ecc44.1750787928.git.nicolinc@nvidia.com
Reviewed-by: Jason Gunthorpe <jgg@nvidia.com>
Signed-off-by: Nicolin Chen <nicolinc@nvidia.com>
Signed-off-by: Jason Gunthorpe <jgg@nvidia.com>
tools/testing/selftests/iommu/iommufd.c

index e61218c0537f22d29fa3a587d4b0d1b099cfcc5f..1926ef6b40abd6e4072b28762e34d497b1c58265 100644 (file)
@@ -1748,13 +1748,15 @@ TEST_F(iommufd_mock_domain, all_aligns)
        unsigned int end;
        uint8_t *buf;
        int prot = PROT_READ | PROT_WRITE;
-       int mfd;
+       int mfd = -1;
 
        if (variant->file)
                buf = memfd_mmap(buf_size, prot, MAP_SHARED, &mfd);
        else
                buf = mmap(0, buf_size, prot, self->mmap_flags, -1, 0);
        ASSERT_NE(MAP_FAILED, buf);
+       if (variant->file)
+               ASSERT_GT(mfd, 0);
        check_refs(buf, buf_size, 0);
 
        /*
@@ -1800,13 +1802,15 @@ TEST_F(iommufd_mock_domain, all_aligns_copy)
        unsigned int end;
        uint8_t *buf;
        int prot = PROT_READ | PROT_WRITE;
-       int mfd;
+       int mfd = -1;
 
        if (variant->file)
                buf = memfd_mmap(buf_size, prot, MAP_SHARED, &mfd);
        else
                buf = mmap(0, buf_size, prot, self->mmap_flags, -1, 0);
        ASSERT_NE(MAP_FAILED, buf);
+       if (variant->file)
+               ASSERT_GT(mfd, 0);
        check_refs(buf, buf_size, 0);
 
        /*