From da53e0d66e70dc20762ef00b360783f6e818ee19 Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Wed, 6 Jul 2011 12:42:56 +0300 Subject: [PATCH] Make rpmLeadRead() return an allocated lead, fixup callers - Requiring callers to allocate a lead "buffer" for reading into is just DUMB (greetings to self back in 2008, sigh). This avoids having to deal with freeing the lead in case the read failed, and allows getting rid of rpmLeadNew() completely. --- lib/package.c | 5 ++--- lib/rpmchecksig.c | 6 +++--- lib/rpmlead.c | 38 +++++++++++++++++++++----------------- lib/rpmlead.h | 11 ++--------- sign/rpmgensig.c | 9 +++------ 5 files changed, 31 insertions(+), 38 deletions(-) diff --git a/lib/package.c b/lib/package.c index 2e8d345..6c16cf1 100644 --- a/lib/package.c +++ b/lib/package.c @@ -560,16 +560,15 @@ static rpmRC rpmpkgRead(rpmKeyring keyring, rpmVSFlags vsflags, if (hdrp) *hdrp = NULL; rpmtdReset(&sigtd); - l = rpmLeadNew(); - if ((rc = rpmLeadRead(fd, l)) == RPMRC_OK) { + if ((rc = rpmLeadRead(fd, &l)) == RPMRC_OK) { const char * err = NULL; if ((rc = rpmLeadCheck(l, &err)) == RPMRC_FAIL) { rpmlog(RPMLOG_ERR, "%s: %s\n", fn, err); } leadtype = rpmLeadType(l); + l = rpmLeadFree(l); } - l = rpmLeadFree(l); if (rc != RPMRC_OK) goto exit; diff --git a/lib/rpmchecksig.c b/lib/rpmchecksig.c index 850ca62..6927308 100644 --- a/lib/rpmchecksig.c +++ b/lib/rpmchecksig.c @@ -268,6 +268,7 @@ static int rpmpkgVerifySigs(rpmKeyring keyring, rpmQueryFlags flags, pgpDigParams sigp; Header sigh = NULL; HeaderIterator hi = NULL; + rpmlead lead = NULL; char * msg = NULL; int res = 1; /* assume failure */ rpmRC rc; @@ -277,14 +278,13 @@ static int rpmpkgVerifySigs(rpmKeyring keyring, rpmQueryFlags flags, rpmDigestBundle plbundle = rpmDigestBundleNew(); rpmDigestBundle hdrbundle = rpmDigestBundleNew(); - rpmlead lead = rpmLeadNew(); - if ((rc = rpmLeadRead(fd, lead)) == RPMRC_OK) { + if ((rc = rpmLeadRead(fd, &lead)) == RPMRC_OK) { const char *lmsg = NULL; rc = rpmLeadCheck(lead, &lmsg); if (rc != RPMRC_OK) rpmlog(RPMLOG_ERR, "%s: %s\n", fn, lmsg); + lead = rpmLeadFree(lead); } - lead = rpmLeadFree(lead); if (rc != RPMRC_OK) { goto exit; diff --git a/lib/rpmlead.c b/lib/rpmlead.c index 8e317b9..bc4ef29 100644 --- a/lib/rpmlead.c +++ b/lib/rpmlead.c @@ -38,11 +38,6 @@ struct rpmlead_s { char reserved[16]; /*!< Pad to 96 bytes -- 8 byte aligned! */ }; -rpmlead rpmLeadNew(void) -{ - return xcalloc(1, sizeof(struct rpmlead_s)); -} - rpmlead rpmLeadFromHeader(Header h) { rpmlead l = NULL; @@ -55,7 +50,7 @@ rpmlead rpmLeadFromHeader(Header h) rpmGetArchInfo(NULL, &archnum); rpmGetOsInfo(NULL, &osnum); - l = rpmLeadNew(); + l = xcalloc(1, sizeof(*l)); l->major = 3; l->minor = 0; l->archnum = archnum; @@ -115,26 +110,35 @@ rpmRC rpmLeadCheck(rpmlead lead, const char **msg) return RPMRC_OK; } -rpmRC rpmLeadRead(FD_t fd, rpmlead lead) +rpmRC rpmLeadRead(FD_t fd, rpmlead *lead) { - assert(lead != NULL); - memset(lead, 0, sizeof(*lead)); - if (Fread(lead, 1, sizeof(*lead), fd) != sizeof(*lead)) { + rpmRC rc = RPMRC_OK; + rpmlead l = xcalloc(1, 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); - return RPMRC_FAIL; + rc = RPMRC_FAIL; } else { rpmlog(RPMLOG_ERR, _("not an rpm package\n")); - return RPMRC_NOTFOUND; + rc = RPMRC_NOTFOUND; } } - lead->type = ntohs(lead->type); - lead->archnum = ntohs(lead->archnum); - lead->osnum = ntohs(lead->osnum); - lead->signature_type = ntohs(lead->signature_type); - return RPMRC_OK; + 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 (rc || lead == NULL) + rpmLeadFree(l); + else + *lead = l; + + return rc; } int rpmLeadType(rpmlead lead) diff --git a/lib/rpmlead.h b/lib/rpmlead.h index 768ea28..1854bd4 100644 --- a/lib/rpmlead.h +++ b/lib/rpmlead.h @@ -21,13 +21,6 @@ extern "C" { typedef struct rpmlead_s * rpmlead; - -/** \ingroup lead - * Initialize a lead structure - * @return Pointer to empty lead structure - */ -rpmlead rpmLeadNew(void); - /** \ingroup lead * Initialize a lead structure from header * param h Header @@ -53,10 +46,10 @@ rpmRC rpmLeadWrite(FD_t fd, rpmlead lead); /** \ingroup lead * Read lead from file handle. * @param fd file handle - * @retval lead package lead + * @retval lead pointer to package lead (malloced) * @return RPMRC_OK on success, RPMRC_FAIL/RPMRC_NOTFOUND on error */ -rpmRC rpmLeadRead(FD_t fd, rpmlead lead); +rpmRC rpmLeadRead(FD_t fd, rpmlead *lead); /** \ingroup lead * Check lead for compatibility. diff --git a/sign/rpmgensig.c b/sign/rpmgensig.c index 4f1099f..6b33254 100644 --- a/sign/rpmgensig.c +++ b/sign/rpmgensig.c @@ -430,7 +430,7 @@ static int rpmSign(const char *rpm, int deleting, const char *passPhrase) { FD_t fd = NULL; FD_t ofd = NULL; - rpmlead lead; + rpmlead lead = NULL; char *sigtarget = NULL, *trpm = NULL; Header sigh = NULL; char * msg; @@ -443,9 +443,7 @@ static int rpmSign(const char *rpm, int deleting, const char *passPhrase) if (manageFile(&fd, rpm, O_RDONLY)) goto exit; - lead = rpmLeadNew(); - - if ((rc = rpmLeadRead(fd, lead)) == RPMRC_OK) { + if ((rc = rpmLeadRead(fd, &lead)) == RPMRC_OK) { const char *lmsg = NULL; rc = rpmLeadCheck(lead, &lmsg); if (rc != RPMRC_OK) @@ -453,7 +451,6 @@ static int rpmSign(const char *rpm, int deleting, const char *passPhrase) } if (rc != RPMRC_OK) { - lead = rpmLeadFree(lead); goto exit; } @@ -552,7 +549,6 @@ static int rpmSign(const char *rpm, int deleting, const char *passPhrase) /* Write the lead/signature of the output rpm */ rc = rpmLeadWrite(ofd, lead); - lead = rpmLeadFree(lead); if (rc != RPMRC_OK) { rpmlog(RPMLOG_ERR, _("%s: writeLead failed: %s\n"), trpm, Fstrerror(ofd)); @@ -584,6 +580,7 @@ exit: if (ofd) (void) closeFile(&ofd); rpmFreeSignature(sigh); + rpmLeadFree(lead); /* Clean up intermediate target */ if (sigtarget) { -- 2.7.4