Allocate directory names on heap in compressFilelist()
authorPanu Matilainen <pmatilai@redhat.com>
Tue, 29 Apr 2008 12:08:47 +0000 (15:08 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Tue, 29 Apr 2008 12:08:47 +0000 (15:08 +0300)
lib/legacy.c

index ae6d7e0..bfd83d9 100644 (file)
@@ -32,7 +32,7 @@ void compressFilelist(Header h)
     HRE_t hre = (HRE_t)headerRemoveEntry;
     HFD_t hfd = headerFreeData;
     char ** fileNames;
-    const char ** dirNames;
+    char ** dirNames;
     const char ** baseNames;
     uint32_t * dirIndexes;
     rpmTagType fnt;
@@ -63,7 +63,7 @@ void compressFilelist(Header h)
     if (fileNames[0][0] != '/') {
        /* HACK. Source RPM, so just do things differently */
        dirIndex = 0;
-       dirNames[dirIndex] = "";
+       dirNames[dirIndex] = xstrdup("");
        for (i = 0; i < count; i++) {
            dirIndexes[i] = dirIndex;
            baseNames[i] = fileNames[i];
@@ -72,7 +72,7 @@ void compressFilelist(Header h)
     }
 
     for (i = 0; i < count; i++) {
-       const char ** needle;
+       char ** needle;
        char savechar;
        char * baseName;
        size_t len;
@@ -86,9 +86,8 @@ void compressFilelist(Header h)
        *baseName = '\0';
        if (dirIndex < 0 ||
            (needle = bsearch(&fileNames[i], dirNames, dirIndex + 1, sizeof(dirNames[0]), dncmp)) == NULL) {
-           char *s = alloca(len + 1);
-           memcpy(s, fileNames[i], len + 1);
-           s[len] = '\0';
+           char *s = xmalloc(len + 1);
+           rstrlcpy(s, fileNames[i], len + 1);
            dirIndexes[i] = ++dirIndex;
            dirNames[dirIndex] = s;
        } else
@@ -108,6 +107,9 @@ exit:
     }
 
     fileNames = hfd(fileNames, fnt);
+    for (i = 0; i <= dirIndex; i++) {
+       free(dirNames[i]);
+    }
     free(dirNames);
     free(baseNames);
     free(dirIndexes);