Skip to contentSkip to conversation

selftests/x86/lam: fix resource leak in do_uring() and allocate_dsa_pasid()

PATCH Superseded Malaya Kumar Rout 1 participant 1 message Mar 25, 2025

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

PATCH 1/1 selftests/x86/lam: fix resource leak in do_uring() and allocate_dsa_pasid()

Exception branch returns without closing
the file descriptors 'file_fd' and 'fd'

---
 tools/testing/selftests/x86/lam.c | 13 +++++++++----
 1 file changed, 9 insertions(+), 4 deletions(-)
tools/testing/selftests/x86/lam.c +9 -4
index 18d736640ece..eaba0a921322 100644
@@ -682,7 +682,7 @@ int do_uring(unsigned long lam)
 		return 1;
 
 	if (fstat(file_fd, &st) < 0)
-		return 1;
+		goto cleanup;
 
 	off_t file_sz = st.st_size;
 
@@ -690,7 +690,7 @@ int do_uring(unsigned long lam)
 
 	fi = malloc(sizeof(*fi) + sizeof(struct iovec) * blocks);
 	if (!fi)
-		return 1;
+		goto cleanup;
 
 	fi->file_sz = file_sz;
 	fi->file_fd = file_fd;
@@ -698,7 +698,7 @@ int do_uring(unsigned long lam)
 	ring = malloc(sizeof(*ring));
 	if (!ring) {
 		free(fi);
-		return 1;
+		goto cleanup;
 	}
 
 	memset(ring, 0, sizeof(struct io_ring));
@@ -730,6 +730,9 @@ int do_uring(unsigned long lam)
 
 	free(fi);
 
+cleanup:
+	close(file_fd);
+
 	return ret;
 }
 
@@ -1189,8 +1192,10 @@ void *allocate_dsa_pasid(void)
 
 	wq = mmap(NULL, 0x1000, PROT_WRITE,
 			   MAP_SHARED | MAP_POPULATE, fd, 0);
-	if (wq == MAP_FAILED)
+	if (wq == MAP_FAILED) {
+		close(fd);
 		perror("mmap");
+	}
 
 	return wq;
 }
Signature · 1 line
2.43.0