Skip to content

Commit 4b75e3b

Browse files
nicolincjgunthorpe
authored andcommitted
iommufd/selftest: Add missing close(mfd) in memfd_mmap()
Do not forget to close mfd in the error paths, since none of the callers would close it when ASSERT_NE(MAP_FAILED, buf) fails. Fixes: 0bcceb1 ("iommufd: Selftest coverage for IOMMU_IOAS_MAP_FILE") Link: https://patch.msgid.link/r/a363a69dbf453d4bc1bde276f3b16778620488e1.1750787928.git.nicolinc@nvidia.com Cc: [email protected] Reviewed-by: Jason Gunthorpe <[email protected]> Signed-off-by: Nicolin Chen <[email protected]> Signed-off-by: Jason Gunthorpe <[email protected]>
1 parent 8186255 commit 4b75e3b

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

tools/testing/selftests/iommu/iommufd_utils.h

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,13 +60,18 @@ static inline void *memfd_mmap(size_t length, int prot, int flags, int *mfd_p)
6060
{
6161
int mfd_flags = (flags & MAP_HUGETLB) ? MFD_HUGETLB : 0;
6262
int mfd = memfd_create("buffer", mfd_flags);
63+
void *buf = MAP_FAILED;
6364

6465
if (mfd <= 0)
6566
return MAP_FAILED;
6667
if (ftruncate(mfd, length))
67-
return MAP_FAILED;
68+
goto out;
6869
*mfd_p = mfd;
69-
return mmap(0, length, prot, flags, mfd, 0);
70+
buf = mmap(0, length, prot, flags, mfd, 0);
71+
out:
72+
if (buf == MAP_FAILED)
73+
close(mfd);
74+
return buf;
7075
}
7176

7277
/*

0 commit comments

Comments
 (0)