Handle HEADERFLAG_SORTED bit correctly in headerUnsort()
authorPanu Matilainen <pmatilai@redhat.com>
Tue, 24 May 2011 11:07:12 +0000 (14:07 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Tue, 24 May 2011 11:07:12 +0000 (14:07 +0300)
- Within rpm there's exactly one caller of headerUnsort() which has
  kinda taken care of re-sorting the header on exit, but only if it
  returns successfully, meaning the header sort status could've been
  left in inconsistent state with implications on consequent
  operations on that header. Also this is part of the public API
  yet callers have no chance of adjusting the flag when they call it
  (and why should they).
- Also dont bother sorting if the header is already in unsorted state.

lib/header.c

index a777f07..38cea37 100644 (file)
@@ -247,7 +247,10 @@ static int offsetCmp(const void * avp, const void * bvp)
  */
 void headerUnsort(Header h)
 {
-    qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
+    if (h->flags & HEADERFLAG_SORTED) {
+       qsort(h->index, h->indexUsed, sizeof(*h->index), offsetCmp);
+       h->flags &= ~HEADERFLAG_SORTED;
+    }
 }
 
 unsigned headerSizeof(Header h, int magicp)
@@ -696,7 +699,6 @@ static void * doHeaderUnload(Header h,
     if (lengthPtr)
        *lengthPtr = len;
 
-    h->flags &= ~HEADERFLAG_SORTED;
     headerSort(h);
 
     return (void *) ei;