ensure list of names is large enough
authorYann Collet <yann.collet.73@gmail.com>
Fri, 19 Apr 2019 00:26:01 +0000 (17:26 -0700)
committerYann Collet <yann.collet.73@gmail.com>
Fri, 19 Apr 2019 00:26:01 +0000 (17:26 -0700)
programs/util.h

index 6a35481..1dd515c 100644 (file)
@@ -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);