subprocess: check and handle fcntl(F_GETFD) failure

Add missing error check for fcntl(fd, F_GETFD, 0) in set_clo_on_exec.
Raise OSError on failure to align with existing FD_SETFD behavior.
This improves robustness in subprocess setup and error visibility.

Github-Pull: arun11299/cpp-subprocess#117
Rebased-From: 9974ff69cdd5fc1a2722cb63f006df9308628b35
This commit is contained in:
Tomás Andróil 2025-04-28 17:14:19 +01:00 committed by Hennadii Stepanov
parent e25ec7300c
commit 7e80030a95
No known key found for this signature in database
GPG key ID: 410108112E7EA81F

View file

@ -346,10 +346,14 @@ namespace util
void set_clo_on_exec(int fd, bool set = true)
{
int flags = fcntl(fd, F_GETFD, 0);
if (flags == -1) {
throw OSError("fcntl F_GETFD failed", errno);
}
if (set) flags |= FD_CLOEXEC;
else flags &= ~FD_CLOEXEC;
//TODO: should check for errors
fcntl(fd, F_SETFD, flags);
if (fcntl(fd, F_SETFD, flags) == -1) {
throw OSError("fcntl F_SETFD failed", errno);
}
}