From a6904d62573904fc16281b0afade67bc4f521bde Mon Sep 17 00:00:00 2001 From: Colin Walters Date: Thu, 1 Feb 2024 13:58:07 -0500 Subject: [PATCH] writer: Close mmap leak Noticed by a static analyzer. Signed-off-by: Colin Walters --- libcomposefs/lcfs-writer.c | 9 ++++++--- 1 file changed, 6 insertions(+), 3 deletions(-) diff --git a/libcomposefs/lcfs-writer.c b/libcomposefs/lcfs-writer.c index e033798..ac8100d 100644 --- a/libcomposefs/lcfs-writer.c +++ b/libcomposefs/lcfs-writer.c @@ -717,18 +717,21 @@ int lcfs_version_from_fd(int fd) { struct lcfs_erofs_header_s *header; - header = mmap(0, sizeof(struct lcfs_erofs_header_s), PROT_READ, - MAP_PRIVATE, fd, 0); + const size_t header_size = sizeof(struct lcfs_erofs_header_s); + header = mmap(0, header_size, PROT_READ, MAP_PRIVATE, fd, 0); if (header == MAP_FAILED) { return -1; } if (lcfs_u32_from_file(header->magic) != LCFS_EROFS_MAGIC || lcfs_u32_from_file(header->version) != LCFS_EROFS_VERSION) { errno = EINVAL; + munmap(header, header_size); return -1; } - return lcfs_u32_from_file(header->composefs_version); + int r = lcfs_u32_from_file(header->composefs_version); + munmap(header, header_size); + return r; } struct lcfs_node_s *lcfs_load_node_from_fd(int fd)