From 9e014460598b10004ff7f9252411a804c32d6ccc Mon Sep 17 00:00:00 2001 From: Panu Matilainen Date: Fri, 27 May 2011 13:59:05 +0300 Subject: [PATCH] Clean up + fix memleaks in readIcon() - Assume failure and use single point of exit where all allocations are freed without dumb dead-assignments. Also fixes a leak from icon allocation when Fread() fails. --- build/parsePreamble.c | 24 ++++++++++-------------- 1 file changed, 10 insertions(+), 14 deletions(-) diff --git a/build/parsePreamble.c b/build/parsePreamble.c index c9f5132..9944513 100644 --- a/build/parsePreamble.c +++ b/build/parsePreamble.c @@ -565,9 +565,9 @@ exit: static rpmRC readIcon(Header h, const char * file) { char *fn = NULL; - uint8_t *icon; - FD_t fd; - rpmRC rc = RPMRC_OK; + uint8_t *icon = NULL; + FD_t fd = NULL; + rpmRC rc = RPMRC_FAIL; /* assume failure */ off_t size; size_t nb, iconsize; @@ -575,17 +575,15 @@ static rpmRC readIcon(Header h, const char * file) fn = rpmGetPath("%{_sourcedir}/", file, NULL); fd = Fopen(fn, "r.ufdio"); - if (fd == NULL || Ferror(fd)) { + if (fd == NULL) { rpmlog(RPMLOG_ERR, _("Unable to open icon %s: %s\n"), fn, Fstrerror(fd)); - rc = RPMRC_FAIL; goto exit; } size = fdSize(fd); iconsize = (size >= 0 ? size : (8 * BUFSIZ)); if (iconsize == 0) { - (void) Fclose(fd); - rc = RPMRC_OK; + rc = RPMRC_OK; /* XXX Eh? */ goto exit; } @@ -596,11 +594,8 @@ static rpmRC readIcon(Header h, const char * file) if (Ferror(fd) || (size >= 0 && nb != size)) { rpmlog(RPMLOG_ERR, _("Unable to read icon %s: %s\n"), fn, Fstrerror(fd)); - rc = RPMRC_FAIL; - } - (void) Fclose(fd); - if (rc != RPMRC_OK) goto exit; + } if (rstreqn((char*)icon, "GIF", sizeof("GIF")-1)) { headerPutBin(h, RPMTAG_GIF, icon, iconsize); @@ -608,13 +603,14 @@ static rpmRC readIcon(Header h, const char * file) headerPutBin(h, RPMTAG_XPM, icon, iconsize); } else { rpmlog(RPMLOG_ERR, _("Unknown icon type: %s\n"), file); - rc = RPMRC_FAIL; goto exit; } - icon = _free(icon); + rc = RPMRC_OK; exit: - fn = _free(fn); + Fclose(fd); + free(fn); + free(icon); return rc; } -- 2.7.4