From 16be0ca4ba53154642bd45e6aa60ffba57369a0c Mon Sep 17 00:00:00 2001 From: Mark Wielaard Date: Sat, 15 Apr 2023 00:13:57 +0200 Subject: [PATCH] tests fdleak.h close all open file descriptors > 2 Use sysconf (_SC_OPEN_MAX) to find the upper limit. Or use 1024 if that fails. https://bugs.kde.org/show_bug.cgi?id=467714 --- NEWS | 2 ++ none/tests/fdleak.h | 12 +++++++++++- 2 files changed, 13 insertions(+), 1 deletion(-) diff --git a/NEWS b/NEWS index 13efee7d4a..6d93d2603e 100644 --- a/NEWS +++ b/NEWS @@ -147,6 +147,8 @@ are not entered into bugzilla tend to get forgotten about or ignored. 465435 m_libcfile.c:66 (vgPlain_safe_fd): Assertion 'newfd >= VG_(fd_hard_limit)' failed. 466104 aligned_alloc problems, part 1 467482 Build failure on aarch64 Alpine +467714 fdleak_* and rlimit tests fail when parent process has more than + 64 descriptors opened 467839 Gdbserver: Improve compatibility of library directory name n-i-bz FreeBSD rfork syscall fail with EINVAL or ENOSYS rather than VG_(unimplemented) diff --git a/none/tests/fdleak.h b/none/tests/fdleak.h index 66fd84e96f..c94dbde319 100644 --- a/none/tests/fdleak.h +++ b/none/tests/fdleak.h @@ -2,6 +2,7 @@ #define _FDLEAK_H_ #include +#include #include #define DO(op) \ @@ -23,6 +24,15 @@ * - For Ubuntu 8.04, see also * https://bugs.launchpad.net/ubuntu/+source/seahorse/+bug/235184 */ -#define CLOSE_INHERITED_FDS { int i; for (i = 3; i < 64; i++) close(i); } +static inline void close_inherited () { + int i; int max_fds = sysconf (_SC_OPEN_MAX); + if (max_fds < 0) + max_fds = 1024; /* Fallback if sysconf fails, returns -1. */ + + /* Only leave 0 (stdin), 1 (stdout) and 2 (stderr) open. */ + for (i = 3; i < max_fds; i++) + close(i); +} +#define CLOSE_INHERITED_FDS close_inherited () #endif /* _FDLEAK_H_ */ -- 2.47.2