public inbox for gcc-patches@gcc.gnu.org
 help / color / mirror / Atom feed
* [committed] libstdc++: Use close-on-exec for file descriptors in filesystem::copy_file
@ 2023-06-06 11:38 Jonathan Wakely
  0 siblings, 0 replies; only message in thread
From: Jonathan Wakely @ 2023-06-06 11:38 UTC (permalink / raw)
  To: libstdc++, gcc-patches

Tested x86_64-linux. Pushed to trunk. This is worth backporting.

-- >8 --

libstdc++-v3/ChangeLog:

	* src/filesystem/ops-common.h (do_copy_file) [O_CLOEXEC]: Set
	close-on-exec flag on file descriptors.
---
 libstdc++-v3/src/filesystem/ops-common.h | 13 +++++++------
 1 file changed, 7 insertions(+), 6 deletions(-)

diff --git a/libstdc++-v3/src/filesystem/ops-common.h b/libstdc++-v3/src/filesystem/ops-common.h
index 901f166514f..f04bbc66d7d 100644
--- a/libstdc++-v3/src/filesystem/ops-common.h
+++ b/libstdc++-v3/src/filesystem/ops-common.h
@@ -515,25 +515,26 @@ _GLIBCXX_BEGIN_NAMESPACE_FILESYSTEM
       int fd;
     };
 
-    int iflag = O_RDONLY;
+    int common_flags = 0;
+#ifdef O_CLOEXEC
+    common_flags |= O_CLOEXEC;
+#endif
 #ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
-    iflag |= O_BINARY;
+    common_flags |= O_BINARY;
 #endif
 
+    const int iflag = O_RDONLY | common_flags;
     CloseFD in = { posix::open(from, iflag) };
     if (in.fd == -1)
       {
 	ec.assign(errno, std::generic_category());
 	return false;
       }
-    int oflag = O_WRONLY|O_CREAT;
+    int oflag = O_WRONLY | O_CREAT | common_flags;
     if (options.overwrite || options.update)
       oflag |= O_TRUNC;
     else
       oflag |= O_EXCL;
-#ifdef _GLIBCXX_FILESYSTEM_IS_WINDOWS
-    oflag |= O_BINARY;
-#endif
     CloseFD out = { posix::open(to, oflag, S_IWUSR) };
     if (out.fd == -1)
       {
-- 
2.40.1


^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2023-06-06 11:38 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-06-06 11:38 [committed] libstdc++: Use close-on-exec for file descriptors in filesystem::copy_file Jonathan Wakely

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).