From 7e80030a952a06101d5755032ebb1ff15823815e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Tom=C3=A1s=20Andr=C3=B3il?= Date: Mon, 28 Apr 2025 17:14:19 +0100 Subject: [PATCH] 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 --- src/util/subprocess.h | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/util/subprocess.h b/src/util/subprocess.h index 29f6e8ffa7e..244a99957c7 100644 --- a/src/util/subprocess.h +++ b/src/util/subprocess.h @@ -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); + } }