From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from gnu.wildebeest.org (gnu.wildebeest.org [45.83.234.184]) by sourceware.org (Postfix) with ESMTPS id 23DC3385DDF1 for ; Sat, 22 Jun 2024 23:50:37 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 23DC3385DDF1 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=klomp.org Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=klomp.org ARC-Filter: OpenARC Filter v1.0.0 sourceware.org 23DC3385DDF1 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=45.83.234.184 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719100238; cv=none; b=lHUqriIIjUTgnD+Mnv9L3oPJAZwalWW+CChf9EhgngDBrndWNU5lng9vANLbScpSRxURqMgnIlL4Sn3Xzmo2fdATYIalfbrrj20rxMJtQ1tQJTX2DCZsuSqzoRU59qLxj5e4ofWTQEEj2ICDSoCOMo3F9KpGCaP9CxkWkekfUyg= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1719100238; c=relaxed/simple; bh=pEpowvNeZ+FaL8QkHf0zZqxji+0d+Pja6M1vIZJytRM=; h=From:To:Subject:Date:Message-ID:MIME-Version; b=cXG6/f+9xQT2iELAvN2etfBePe7ymbUhq8134M/bD+9ZMCgCz4x+6zrDACoIZHXTgmpnq3XmaV4uL7deGd17F7/Fa+nEt1HAWTTB7IPr1tD+wTfatkMHAJSiu6OsN1p8Phoow2b78ZuS1YDjj4npPAXrHVUSOi9XWReibU7Tp6M= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from mwielaar-thinkpadp1gen3.rmtnl.csb (deer0x08.wildebeest.org [172.31.17.138]) (using TLSv1.2 with cipher ADH-AES256-GCM-SHA384 (256/256 bits)) (No client certificate requested) by gnu.wildebeest.org (Postfix) with ESMTPSA id F2CFC3032F85; Sun, 23 Jun 2024 01:50:35 +0200 (CEST) Received: by mwielaar-thinkpadp1gen3.rmtnl.csb (Postfix, from userid 10916) id DD1001A6255; Sun, 23 Jun 2024 01:50:35 +0200 (CEST) From: Mark Wielaard To: elfutils-devel@sourceware.org Cc: Mark Wielaard Subject: [PATCH 4/4] ar, ranlib: Don't double close file descriptors Date: Sun, 23 Jun 2024 01:50:13 +0200 Message-ID: <20240622235013.2071424-4-mark@klomp.org> X-Mailer: git-send-email 2.45.2 In-Reply-To: <20240622235013.2071424-1-mark@klomp.org> References: <20240622235013.2071424-1-mark@klomp.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-8.9 required=5.0 tests=BAYES_00,GIT_PATCH_0,JMQ_SPF_NEUTRAL,KAM_DMARC_STATUS,SPF_HELO_NONE,SPF_PASS,TXREP autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: Found by GCC14 -Wanalyzer-fd-double-close. close always closes the given file descriptor even on error. So don't try to close a file descriptor again on error (even on EINTR). This could be bad in a multi-threaded environment. * src/ar.c (do_oper_extract): Call close and set newfd to -1. (do_oper_delete): Likewise. (do_oper_insert): Likewise. * src/ranlib.c (handle_file): Likewise. Signed-off-by: Mark Wielaard --- src/ar.c | 12 ++++++------ src/ranlib.c | 4 ++-- 2 files changed, 8 insertions(+), 8 deletions(-) diff --git a/src/ar.c b/src/ar.c index fcb8bfb90a9f..9ace28b918d3 100644 --- a/src/ar.c +++ b/src/ar.c @@ -808,9 +808,9 @@ cannot rename temporary file to %.*s"), if (fchown (newfd, st.st_uid, st.st_gid) != 0) { ; } /* Set the mode of the new file to the same values the original file has. */ - if (fchmod (newfd, st.st_mode & ALLPERMS) != 0 - || close (newfd) != 0) + if (fchmod (newfd, st.st_mode & ALLPERMS) != 0) goto nonew_unlink; + close (newfd); newfd = -1; if (rename (tmpfname, arfname) != 0) goto nonew_unlink; @@ -1061,9 +1061,9 @@ do_oper_delete (const char *arfname, char **argv, int argc, setting the mode (which might be reset/ignored if the owner is wrong. */ if (fchown (newfd, st.st_uid, st.st_gid) != 0) { ; } - if (fchmod (newfd, st.st_mode & ALLPERMS) != 0 - || close (newfd) != 0) + if (fchmod (newfd, st.st_mode & ALLPERMS) != 0) goto nonew_unlink; + close (newfd); newfd = -1; if (rename (tmpfname, arfname) != 0) goto nonew_unlink; @@ -1547,9 +1547,9 @@ do_oper_insert (int oper, const char *arfname, char **argv, int argc, setting the modes, or they might be reset/ignored if the owner is wrong. */ if (fchown (newfd, st.st_uid, st.st_gid) != 0) { ; } - if (fchmod (newfd, st.st_mode & ALLPERMS) != 0 - || close (newfd) != 0) + if (fchmod (newfd, st.st_mode & ALLPERMS) != 0) goto nonew_unlink; + close (newfd); newfd = -1; if (rename (tmpfname, arfname) != 0) goto nonew_unlink; diff --git a/src/ranlib.c b/src/ranlib.c index 7838d69eaec6..073df8c551af 100644 --- a/src/ranlib.c +++ b/src/ranlib.c @@ -264,9 +264,9 @@ handle_file (const char *fname) if (fchown (newfd, st.st_uid, st.st_gid) != 0) { ; } /* Set the mode of the new file to the same values the original file has. */ - if (fchmod (newfd, st.st_mode & ALLPERMS) != 0 - || close (newfd) != 0) + if (fchmod (newfd, st.st_mode & ALLPERMS) != 0) goto nonew_unlink; + close (newfd); newfd = -1; if (rename (tmpfname, fname) != 0) goto nonew_unlink; -- 2.45.2