]> git.ipfire.org Git - thirdparty/zstd.git/commitdiff
visual studio fix
authorYann Collet <cyan@fb.com>
Tue, 25 Mar 2025 22:22:55 +0000 (15:22 -0700)
committerYann Collet <cyan@fb.com>
Fri, 28 Mar 2025 16:18:17 +0000 (09:18 -0700)
programs/util.c

index 43f2b9408c7eaa0061015c6fa3ed708f74d7fd91..94c40cc958b811bd77efe68aaf22a0594a87be25 100644 (file)
@@ -703,21 +703,34 @@ static size_t UTIL_processLines(char* buffer, size_t bufferSize)
 }
 
 /* Create an array of pointers to the lines in a buffer */
-static const char**
-UTIL_createLinePointers(char* buffer, size_t numLines, size_t bufferSize)
+static const char** UTIL_createLinePointers(char* buffer, size_t numLines, size_t bufferSize)
 {
     size_t lineIndex = 0;
     size_t pos = 0;
-    const char** linePointers = (const char**)malloc(numLines * sizeof(*linePointers));
-    if (linePointers == NULL) return NULL;
+    void* const bufferPtrs = malloc(numLines * sizeof(const char**));
+    const char** const linePointers = (const char**)bufferPtrs;
+    if (bufferPtrs == NULL) return NULL;
 
     while (lineIndex < numLines && pos < bufferSize) {
-        linePointers[lineIndex++] = buffer + pos;
-        pos += strlen(buffer + pos) + 1;  /* +1 for the finishing `\0` */
+        size_t len = 0;
+        linePointers[lineIndex++] = buffer+pos;
+
+        /* Find the next null terminator, being careful not to go past the buffer */
+        while ((pos + len < bufferSize) && buffer[pos + len] != '\0') {
+            len++;
+        }
+
+        /* Move past this string and its null terminator */
+        pos += len;
+        if (pos < bufferSize) pos++;  /* Skip the null terminator if we're not at buffer end */
     }
 
-    assert(pos <= bufferSize);
-    assert(lineIndex == numLines);
+    /* Verify we processed the expected number of lines */
+    if (lineIndex != numLines) {
+        /* Something went wrong - we didn't find as many lines as expected */
+        free(bufferPtrs);
+        return NULL;
+    }
 
     return linePointers;
 }