From 564f3cc77aa7a4d5e650e351d79c15e50c6339cb Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Sun, 23 Jun 2024 01:29:42 +0200 Subject: [PATCH] ar, ranlib: Don't double close file descriptors 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 fcb8bfb90..9ace28b91 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 7838d69ea..073df8c55 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.47.3