bug#79415: [PATCH] Fix tmpfile close on execute
[email protected] Tue, 9 Sep 2025 13:35:00 -0400
| Newsgroups | gmane.lisp.guile.bugs |
|---|---|
| Message-ID | <[email protected]> |
From: Olivier Dion <[email protected]> Guile duplicates the underlying file descriptor stored in a FILE pointer returned by tmpfile(3). This is done to manage the file descriptor with Guile's ports abstraction instead of using the C FILE pointer abstraction. However, the dup(2) system call does not copy the file descriptor flags (O_CLOEXEC), which is important for respecting the semantic expected from tmpfile(3) in that the temporary file will not leak to child processes. Fix this by using the fcntl(2) system call using the `F_DUPFD_CLOEXEC' command. * libguile/posix.c (scm_tmpfile): Use fcntl(2) instead of dup(2) on none MINGW32 systems. --- libguile/posix.c | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/libguile/posix.c b/libguile/posix.c index c8bbb0f83..fc9a116ed 100644 --- a/libguile/posix.c +++ b/libguile/posix.c @@ -1825,7 +1825,19 @@ SCM_DEFINE (scm_tmpfile, "tmpfile", 0, 0, 0, SCM_SYSERROR; #ifndef __MINGW32__ - fd = dup (fileno (rv)); + /* tmpfile(3) says: + [...] the file will be automatically deleted when it is closed + or the program terminates. + + That last part can only be done if the `O_CLOEXEC' file descriptor + flag is set. + + However, dup(2) does not copy the file descriptor flags to the new + file descriptor. fcntl(2) with the `F_DUPFD_CLOEXEC' command + essentially to the same thing as dup(2) but will set the + `O_CLOEXEC' flag on the new file descriptor, thus preserving the + expected behavior. */ + fd = fcntl (fileno (rv), F_DUPFD_CLOEXEC, 0); fclose (rv); #else fd = fileno (rv); -- 2.50.1