From 4e4f1ad623cca9ebd212400b5783c63fd76dc868 Mon Sep 17 00:00:00 2001 From: Yann Collet Date: Thu, 18 Apr 2019 17:26:01 -0700 Subject: ensure list of names is large enough --- programs/util.h | 19 +++++++++---------- 1 file changed, 9 insertions(+), 10 deletions(-) diff --git a/programs/util.h b/programs/util.h index 6a35481..1dd515c 100644 --- a/programs/util.h +++ b/programs/util.h @@ -379,7 +379,7 @@ UTIL_STATIC U64 UTIL_getTotalFileSize(const char** fileNamesTable, unsigned nbFi */ UTIL_STATIC void* UTIL_realloc(void* ptr, size_t size) { - void* newptr = realloc(ptr, size); + void* const newptr = realloc(ptr, size); if (newptr) return newptr; free(ptr); return NULL; @@ -529,7 +529,8 @@ UTIL_STATIC int UTIL_prepareFileList(const char* dirName, char** bufStart, size_ * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called. */ UTIL_STATIC const char** -UTIL_createFileList(const char** inputNames, unsigned inputNamesNb, char** allocatedBuffer, unsigned* allocatedNamesNb) +UTIL_createFileList(const char** inputNames, unsigned inputNamesNb, + char** allocatedBuffer, unsigned* allocatedNamesNb) { size_t pos; unsigned i, nbFiles; @@ -543,16 +544,14 @@ UTIL_createFileList(const char** inputNames, unsigned inputNamesNb, char** alloc if (!UTIL_isDirectory(inputNames[i])) { size_t const len = strlen(inputNames[i]); if (pos + len >= bufSize) { - size_t newListSize = bufSize + LIST_SIZE_INCREASE; - buf = (char*)UTIL_realloc(buf, newListSize); - bufSize = newListSize; + while (pos + len >= bufSize) bufSize += LIST_SIZE_INCREASE; + buf = (char*)UTIL_realloc(buf, bufSize); if (!buf) return NULL; } - if (pos + len < bufSize) { - strncpy(buf + pos, inputNames[i], bufSize - pos); - pos += len + 1; - nbFiles++; - } + assert(pos + len < bufSize); + strncpy(buf + pos, inputNames[i], bufSize - pos); + pos += len + 1; + nbFiles++; } else { char* bufend = buf + bufSize; nbFiles += (unsigned)UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend); -- cgit v0.12