From 4f0ad83372a80cd86f7edacd5a3e32974b4879b7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E6=BA=90=E6=96=87=E9=9B=A8?= <41315874+fumiama@users.noreply.github.com> Date: Fri, 1 Dec 2023 11:01:18 +0900 Subject: [PATCH] fix(file.h): close fd on error --- file.h | 3 +++ 1 file changed, 3 insertions(+) diff --git a/file.h b/file.h index 7bf2858..9021317 100644 --- a/file.h +++ b/file.h @@ -38,11 +38,13 @@ int file_cache_init(file_cache_t* fc, char* path) { } if(fstat(fd, &sb) < 0) { perror("fstat"); + close(fd); return -3; } if(sb.st_size < page_size) { if(ftruncate(fd, page_size) < 0) { perror("ftruncate"); + close(fd); return -4; } sb.st_size = page_size; @@ -125,6 +127,7 @@ int file_cache_realloc(file_cache_t* fc, uint64_t newsize) { fc->size = (size_t)newsize + sizeof(uint64_t); if(ftruncate(fd, fc->size) < 0) { perror("ftruncate"); + close(fd); return -4; } fc->data = mmap(NULL, fc->size, PROT_READ|PROT_WRITE, MAP_SHARED, fd, 0);