Only bother mallocing lead for return if read actually succeeded
authorPanu Matilainen <Panu Matilainen pmatilai@redhat.com>
Thu, 7 Jul 2011 08:17:29 +0000 (11:17 +0300)
committerPanu Matilainen <Panu Matilainen pmatilai@redhat.com>
Thu, 7 Jul 2011 08:17:29 +0000 (11:17 +0300)
- Doesn't make much of a difference now, just paving way for next steps

lib/rpmlead.c

index 6ad2c37..ad47722 100644 (file)
@@ -113,9 +113,10 @@ rpmRC rpmLeadCheck(rpmlead lead, const char **msg)
 rpmRC rpmLeadRead(FD_t fd, rpmlead *lead)
 {
     rpmRC rc = RPMRC_OK;
-    rpmlead l = xcalloc(1, sizeof(*l));
+    struct rpmlead_s l;
 
-    if (Fread(l, 1, sizeof(*l), fd) != sizeof(*l)) {
+    memset(&l, 0, sizeof(l));
+    if (Fread(&l, 1, sizeof(l), fd) != sizeof(l)) {
        if (Ferror(fd)) {
            rpmlog(RPMLOG_ERR, _("read failed: %s (%d)\n"),
                        Fstrerror(fd), errno);
@@ -124,20 +125,18 @@ rpmRC rpmLeadRead(FD_t fd, rpmlead *lead)
            rpmlog(RPMLOG_ERR, _("not an rpm package\n"));
            rc = RPMRC_NOTFOUND;
        }
+    } else {
+       l.type = ntohs(l.type);
+       l.archnum = ntohs(l.archnum);
+       l.osnum = ntohs(l.osnum);
+       l.signature_type = ntohs(l.signature_type);
     }
 
-    if (rc == RPMRC_OK) {
-       l->type = ntohs(l->type);
-       l->archnum = ntohs(l->archnum);
-       l->osnum = ntohs(l->osnum);
-       l->signature_type = ntohs(l->signature_type);
+    if (lead != NULL && rc == RPMRC_OK) {
+       *lead = xmalloc(sizeof(l));
+       memcpy(*lead, &l, sizeof(l));
     }
 
-    if (rc || lead == NULL)
-       rpmLeadFree(l);
-    else
-       *lead = l;
-
     return rc;
 }