Set HEADERFLAGS_ALLOCATED centrally from headerLoad()
authorPanu Matilainen <pmatilai@redhat.com>
Tue, 23 Jun 2009 14:50:08 +0000 (17:50 +0300)
committerPanu Matilainen <pmatilai@redhat.com>
Tue, 23 Jun 2009 14:50:08 +0000 (17:50 +0300)
- requiring every caller to handle this separately, violating header type
  opaqueness in the process, doesn't seem that bright an idea
- also fix a memleak on signature header read in case of failure

lib/header.c
lib/package.c
lib/rpmdb.c
lib/signature.c

index 8cac975..e89de64 100644 (file)
@@ -966,6 +966,7 @@ Header headerLoad(void * uh)
 
     h->flags &= ~HEADERFLAG_SORTED;
     headerSort(h);
+    h->flags |= HEADERFLAG_ALLOCATED;
 
     return h;
 
@@ -991,9 +992,6 @@ Header headerReload(Header h, rpmTag tag)
        uh = _free(uh);
        return NULL;
     }
-    if (nh->flags & HEADERFLAG_ALLOCATED)
-       uh = _free(uh);
-    nh->flags |= HEADERFLAG_ALLOCATED;
     if (ENTRY_IS_REGION(nh->index)) {
        if (tag == HEADER_SIGNATURES || tag == HEADER_IMMUTABLE)
            nh->index[0].info.tag = tag;
@@ -1014,11 +1012,9 @@ Header headerCopyLoad(const void * uh)
     /* Sanity checks on header intro. */
     if (!(hdrchkTags(il) || hdrchkData(dl)) && pvlen < headerMaxbytes) {
        nuh = memcpy(xmalloc(pvlen), uh, pvlen);
-       if ((h = headerLoad(nuh)) != NULL)
-           h->flags |= HEADERFLAG_ALLOCATED;
+       if ((h = headerLoad(nuh)) == NULL)
+           nuh = _free(nuh);
     }
-    if (h == NULL)
-       nuh = _free(nuh);
     return h;
 }
 
@@ -1079,12 +1075,9 @@ Header headerRead(FD_t fd, enum hMagic magicp)
     h = headerLoad(ei);
 
 exit:
-    if (h) {
-       if (h->flags & HEADERFLAG_ALLOCATED)
-           ei = _free(ei);
-       h->flags |= HEADERFLAG_ALLOCATED;
-    } else if (ei)
-       ei = _free(ei);
+    if (h == NULL && ei != NULL) {
+       free(ei);
+    }
     return h;
 }
 
index ebb1135..6cff7c1 100644 (file)
@@ -511,7 +511,6 @@ static rpmRC rpmpkgReadHeader(rpmKeyring keyring, rpmVSFlags vsflags,
        rc = RPMRC_FAIL;
         goto exit;
     }
-    h->flags |= HEADERFLAG_ALLOCATED;
     ei = NULL; /* XXX will be freed with header */
     
 exit:
index d76630e..1f035bf 100644 (file)
@@ -29,7 +29,7 @@
 
 #include "lib/rpmdb_internal.h"
 #include "lib/fprint.h"
-#include "lib/header_internal.h"       /* XXX for HEADERFLAG_ALLOCATED */
+#include "lib/header_internal.h"       /* XXX for headerSetInstance() */
 #include "debug.h"
 
 int _rpmdb_debug = 0;
@@ -2097,8 +2097,6 @@ top:
     /* Did the header blob load correctly? */
 #if !defined(_USE_COPY_LOAD)
     mi->mi_h = headerLoad(uh);
-    if (mi->mi_h)
-       mi->mi_h->flags |= HEADERFLAG_ALLOCATED;
 #else
     mi->mi_h = headerCopyLoad(uh);
 #endif
index 808bce7..a6d1efd 100644 (file)
@@ -236,7 +236,6 @@ rpmRC rpmReadSignature(FD_t fd, Header * sighp, sigType sig_type, char ** msg)
        rasprintf(&buf, _("sigh load: BAD\n"));
        goto exit;
     }
-    sigh->flags |= HEADERFLAG_ALLOCATED;
 
     {  size_t sigSize = headerSizeof(sigh, HEADER_MAGIC_YES);
        size_t pad = (8 - (sigSize % 8)) % 8; /* 8-byte pad */
@@ -267,11 +266,13 @@ rpmRC rpmReadSignature(FD_t fd, Header * sighp, sigType sig_type, char ** msg)
            goto exit;
        }
     }
+    ei = NULL; /* XXX will be freed with header */
 
 exit:
     if (sighp && sigh && rc == RPMRC_OK)
        *sighp = headerLink(sigh);
     sigh = headerFree(sigh);
+    free(ei);
 
     if (msg != NULL) {
        *msg = buf;