Skip to content

Commit 99d55e5

Browse files
JanWielemakerclaude
andcommitted
FIXED: setCloseOnExec() silently failed on missing fd / failing fcntl
set_stream/2 close_on_exec now raises a permission_error when the stream has no OS file descriptor and a system_error (preserving errno) when fcntl()/SetHandleInformation() fails, instead of failing silently. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 4e8f389 commit 99d55e5

1 file changed

Lines changed: 6 additions & 6 deletions

File tree

src/os/pl-file.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2105,29 +2105,29 @@ PRED_IMPL("noprotocol", 0, noprotocol, 0)
21052105
*******************************/
21062106

21072107
static bool
2108-
setCloseOnExec(IOSTREAM *s, bool val)
2108+
setCloseOnExec(IOSTREAM *s, term_t stream, bool val)
21092109
{ int fd;
21102110

21112111
if ( (fd = Sfileno(s)) < 0)
2112-
return false;
2112+
return PL_permission_error("close_on_exec", "stream", stream);
21132113

21142114
#if defined(F_SETFD) && defined(FD_CLOEXEC)
21152115
{ int fd_flags = fcntl(fd, F_GETFD);
21162116

21172117
if ( fd_flags == -1 )
2118-
return false;
2118+
return PL_error(NULL, 0, MSG_ERRNO, ERR_SYSCALL, "fcntl");
21192119
if ( val )
21202120
fd_flags |= FD_CLOEXEC;
21212121
else
21222122
fd_flags &= ~FD_CLOEXEC;
21232123

21242124
if ( fcntl(fd, F_SETFD, fd_flags) == -1 )
2125-
return false;
2125+
return PL_error(NULL, 0, MSG_ERRNO, ERR_SYSCALL, "fcntl");
21262126
}
21272127
#elif defined __WINDOWS__
21282128
{ if ( !SetHandleInformation((HANDLE)_get_osfhandle(fd),
21292129
HANDLE_FLAG_INHERIT, !val) )
2130-
return false;
2130+
return PL_error(NULL, 0, MSG_ERRNO, ERR_SYSCALL, "SetHandleInformation");
21312131
}
21322132
#else
21332133
return PL_error(NULL, 0, NULL, ERR_NOT_IMPLEMENTED, "close_on_exec");
@@ -2454,7 +2454,7 @@ set_stream(DECL_LD IOSTREAM *s, term_t stream, atom_t aname, term_t a)
24542454
if ( !PL_get_stdbool_ex(a, &val) )
24552455
return false;
24562456

2457-
return setCloseOnExec(s, val);
2457+
return setCloseOnExec(s, stream, val);
24582458
} else
24592459
{ assert(0);
24602460
return false;

0 commit comments

Comments
 (0)