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-05-01 22:16:11 +01:00 committed by Hennadii Stepanov
parent b7288decdf
commit cd95c9d6a7
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) void set_clo_on_exec(int fd, bool set = true)
{ {
int flags = fcntl(fd, F_GETFD, 0); int flags = fcntl(fd, F_GETFD, 0);
if (flags == -1) {
throw OSError("fcntl F_GETFD failed", errno);
}
if (set) flags |= FD_CLOEXEC; if (set) flags |= FD_CLOEXEC;
else flags &= ~FD_CLOEXEC; else flags &= ~FD_CLOEXEC;
//TODO: should check for errors if (fcntl(fd, F_SETFD, flags) == -1) {
fcntl(fd, F_SETFD, flags); throw OSError("fcntl F_SETFD failed", errno);
}
} }