Lubos Kardos f54527
From 9c36ca411332d2718eca339e867561c39abc256b Mon Sep 17 00:00:00 2001
Lubos Kardos f54527
From: Lubos Kardos <lkardos@redhat.com>
Lubos Kardos f54527
Date: Fri, 6 Nov 2015 14:49:59 +0100
Lubos Kardos f54527
Subject: [PATCH] Fix crash when parsing corrupted RPM file (rhbz:1273360)
Lubos Kardos f54527
Lubos Kardos f54527
---
Lubos Kardos f54527
 lib/legacy.c | 14 ++++++++------
Lubos Kardos f54527
 1 file changed, 8 insertions(+), 6 deletions(-)
Lubos Kardos f54527
Lubos Kardos f54527
diff --git a/lib/legacy.c b/lib/legacy.c
Lubos Kardos f54527
index 422c2b0..8ba7bbd 100644
Lubos Kardos f54527
--- a/lib/legacy.c
Lubos Kardos f54527
+++ b/lib/legacy.c
Lubos Kardos f54527
@@ -25,7 +25,7 @@ static void compressFilelist(Header h)
Lubos Kardos f54527
     char ** dirNames;
Lubos Kardos f54527
     const char ** baseNames;
Lubos Kardos f54527
     uint32_t * dirIndexes;
Lubos Kardos f54527
-    rpm_count_t count;
Lubos Kardos f54527
+    rpm_count_t count, realCount = 0;
Lubos Kardos f54527
     int i;
Lubos Kardos f54527
     int dirIndex = -1;
Lubos Kardos f54527
 
Lubos Kardos f54527
@@ -58,6 +58,7 @@ static void compressFilelist(Header h)
Lubos Kardos f54527
 	    while ((i = rpmtdNext(&fileNames)) >= 0) {
Lubos Kardos f54527
 		dirIndexes[i] = dirIndex;
Lubos Kardos f54527
 		baseNames[i] = rpmtdGetString(&fileNames);
Lubos Kardos f54527
+		realCount++;
Lubos Kardos f54527
 	    }
Lubos Kardos f54527
 	    goto exit;
Lubos Kardos f54527
 	}
Lubos Kardos f54527
@@ -87,19 +88,20 @@ static void compressFilelist(Header h)
Lubos Kardos f54527
 	    (needle = bsearch(&filename, dirNames, dirIndex + 1, sizeof(dirNames[0]), dncmp)) == NULL) {
Lubos Kardos f54527
 	    char *s = xmalloc(len + 1);
Lubos Kardos f54527
 	    rstrlcpy(s, filename, len + 1);
Lubos Kardos f54527
-	    dirIndexes[i] = ++dirIndex;
Lubos Kardos f54527
+	    dirIndexes[realCount] = ++dirIndex;
Lubos Kardos f54527
 	    dirNames[dirIndex] = s;
Lubos Kardos f54527
 	} else
Lubos Kardos f54527
-	    dirIndexes[i] = needle - dirNames;
Lubos Kardos f54527
+	    dirIndexes[realCount] = needle - dirNames;
Lubos Kardos f54527
 
Lubos Kardos f54527
 	*baseName = savechar;
Lubos Kardos f54527
-	baseNames[i] = baseName;
Lubos Kardos f54527
+	baseNames[realCount] = baseName;
Lubos Kardos f54527
+	realCount++;
Lubos Kardos f54527
     }
Lubos Kardos f54527
 
Lubos Kardos f54527
 exit:
Lubos Kardos f54527
     if (count > 0) {
Lubos Kardos f54527
-	headerPutUint32(h, RPMTAG_DIRINDEXES, dirIndexes, count);
Lubos Kardos f54527
-	headerPutStringArray(h, RPMTAG_BASENAMES, baseNames, count);
Lubos Kardos f54527
+	headerPutUint32(h, RPMTAG_DIRINDEXES, dirIndexes, realCount);
Lubos Kardos f54527
+	headerPutStringArray(h, RPMTAG_BASENAMES, baseNames, realCount);
Lubos Kardos f54527
 	headerPutStringArray(h, RPMTAG_DIRNAMES, 
Lubos Kardos f54527
 			     (const char **) dirNames, dirIndex + 1);
Lubos Kardos f54527
     }
Lubos Kardos f54527
-- 
Lubos Kardos f54527
1.9.3
Lubos Kardos f54527