]> git.ipfire.org Git - thirdparty/elfutils.git/commitdiff
ar, ranlib: Don't double close file descriptors
authorMark Wielaard <mark@klomp.org>
Sat, 22 Jun 2024 23:29:42 +0000 (01:29 +0200)
committerMark Wielaard <mark@klomp.org>
Sat, 22 Jun 2024 23:30:57 +0000 (01:30 +0200)
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 <mark@klomp.org>
src/ar.c
src/ranlib.c

index fcb8bfb90a9f8e3bbf92fc2a783e4489eb3c9441..9ace28b918d38e19caec3f4d4d99d9e46911741e 100644 (file)
--- 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;
index 7838d69eaec6826f26111de66794df77c1f3fd5b..073df8c551afd006a839ba2ca7898c02bcdb461f 100644 (file)
@@ -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;