From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from dormouse.elm.relay.mailchannels.net (dormouse.elm.relay.mailchannels.net [23.83.212.50]) by sourceware.org (Postfix) with ESMTPS id 0E9623858039 for ; Mon, 10 May 2021 15:58:41 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.3.2 sourceware.org 0E9623858039 X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from relay.mailchannels.net (localhost [127.0.0.1]) by relay.mailchannels.net (Postfix) with ESMTP id 4E86C542CAE for ; Mon, 10 May 2021 15:58:40 +0000 (UTC) Received: from pdx1-sub0-mail-a48.g.dreamhost.com (100-98-55-119.trex.outbound.svc.cluster.local [100.98.55.119]) (Authenticated sender: dreamhost) by relay.mailchannels.net (Postfix) with ESMTPA id E235A542D23 for ; Mon, 10 May 2021 15:58:39 +0000 (UTC) X-Sender-Id: dreamhost|x-authsender|siddhesh@gotplt.org Received: from pdx1-sub0-mail-a48.g.dreamhost.com (pop.dreamhost.com [64.90.62.162]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384) by 100.98.55.119 (trex/6.2.1); Mon, 10 May 2021 15:58:40 +0000 X-MC-Relay: Neutral X-MailChannels-SenderId: dreamhost|x-authsender|siddhesh@gotplt.org X-MailChannels-Auth-Id: dreamhost X-Inform-Madly: 6e0398cc189406b1_1620662320130_4032096374 X-MC-Loop-Signature: 1620662320129:806292558 X-MC-Ingress-Time: 1620662320129 Received: from pdx1-sub0-mail-a48.g.dreamhost.com (localhost [127.0.0.1]) by pdx1-sub0-mail-a48.g.dreamhost.com (Postfix) with ESMTP id 87B7E854BE for ; Mon, 10 May 2021 08:58:39 -0700 (PDT) Received: from rhbox.intra.reserved-bit.com (unknown [1.186.101.110]) (using TLSv1.2 with cipher ECDHE-RSA-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) (Authenticated sender: siddhesh@gotplt.org) by pdx1-sub0-mail-a48.g.dreamhost.com (Postfix) with ESMTPSA id 3D1138515F for ; Mon, 10 May 2021 08:58:37 -0700 (PDT) X-DH-BACKEND: pdx1-sub0-mail-a48 From: Siddhesh Poyarekar To: libc-alpha@sourceware.org Subject: [PATCH] elf/cache.c: Fix resource leaks identified by static analyzers Date: Mon, 10 May 2021 21:28:27 +0530 Message-Id: <20210510155827.131051-1-siddhesh@sourceware.org> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-3494.8 required=5.0 tests=BAYES_00, GIT_PATCH_0, JMQ_SPF_NEUTRAL, KAM_DMARC_NONE, KAM_DMARC_STATUS, RCVD_IN_BARRACUDACENTRAL, RCVD_IN_DNSWL_NONE, RCVD_IN_MSPIKE_H2, SPF_HELO_NONE, SPF_NEUTRAL, TXREP autolearn=ham autolearn_force=no version=3.4.2 X-Spam-Checker-Version: SpamAssassin 3.4.2 (2018-09-13) on server2.sourceware.org X-BeenThere: libc-alpha@sourceware.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Libc-alpha mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Mon, 10 May 2021 15:58:43 -0000 A coverity run identified a number of resource leaks in cache.c. There are a couple of simple memory leaks where a local allocation is not freed before function return. Then there is a mmap leak and a file descriptor leak where a map is not unmapped in the error case and a file descriptor remains open respectively. --- elf/cache.c | 16 ++++++++++++---- 1 file changed, 12 insertions(+), 4 deletions(-) diff --git a/elf/cache.c b/elf/cache.c index c01d302072..8a3404923c 100644 --- a/elf/cache.c +++ b/elf/cache.c @@ -547,6 +547,7 @@ write_extensions (int fd, uint32_t str_offset, || write (fd, generator, strlen (generator)) !=3D strlen (generato= r)) error (EXIT_FAILURE, errno, _("Writing of cache extension data faile= d")); =20 + free (hwcaps_array); free (ext); } =20 @@ -778,6 +779,7 @@ save_cache (const char *cache_name) free (file_entries_new); free (file_entries); free (strings_finalized.strings); + free (temp_name); =20 while (entries) { @@ -1034,6 +1036,9 @@ load_aux_cache (const char *aux_cache_name) + aux_cache->nlibs * sizeof (struct aux_cache_file_entry) + aux_cache->len_strings)) { + if (aux_cache !=3D MAP_FAILED) + munmap (aux_cache, aux_cache_size); + close (fd); init_aux_cache (); return; @@ -1143,10 +1148,13 @@ save_aux_cache (const char *aux_cache_name) if (fd < 0) goto out_fail; =20 - if (write (fd, file_entries, file_entries_size + total_strlen) - !=3D (ssize_t) (file_entries_size + total_strlen) - || fdatasync (fd) !=3D 0 - || close (fd) !=3D 0) + bool fail =3D ((write (fd, file_entries, file_entries_size + total_str= len) + !=3D (ssize_t) (file_entries_size + total_strlen)) + || fdatasync (fd) !=3D 0); + + fail |=3D close (fd) !=3D 0; + + if (fail) { unlink (temp_name); goto out_fail; --=20 2.31.1