From 5f24f87c37d96a9f77db8d5965289386ed2dfbdd Mon Sep 17 00:00:00 2001 From: richardsonnick Date: Tue, 23 Dec 2025 16:29:27 -0500 Subject: [PATCH] fix potential invalid mmap call and uninitialized file size Fixes a warning from Clang 21 (unix.StdCLibraryFunctions) where mmap() could be called with a length of 0 if the dictionary file is empty. Mapping a zero-length range is undefined behavior under POSIX. - Added guard for zero-length dictionary files. --- programs/fileio.c | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/programs/fileio.c b/programs/fileio.c index 4000c5b62f4..d13a2984b89 100644 --- a/programs/fileio.c +++ b/programs/fileio.c @@ -1040,6 +1040,7 @@ static size_t FIO_setDictBufferMMap(FIO_Dict_t* dict, const char* fileName, FIO_ EXM_THROW(33, "Couldn't open dictionary %s: %s", fileName, strerror(errno)); } + // filesize could be garbage if UTIL_getFileSizeStat fails fileSize = UTIL_getFileSizeStat(dictFileStat); { size_t const dictSizeMax = prefs->patchFromMode ? prefs->memLimit : DICTSIZE_MAX; @@ -1049,6 +1050,11 @@ static size_t FIO_setDictBufferMMap(FIO_Dict_t* dict, const char* fileName, FIO_ } } + if (fileSize == 0) return 0; + if (fileSize == (unsigned long long)-1) { + EXM_THROW(35, "Could not determine size of dictionary %s", fileName); + } + *bufferPtr = mmap(NULL, (size_t)fileSize, PROT_READ, MAP_PRIVATE, fileHandle, 0); if (*bufferPtr==NULL) EXM_THROW(34, "%s", strerror(errno));