From f4780f85ae02fa32141cd56aa759d041ed4b27cf Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 14 Nov 2017 14:42:38 +0100 Subject: [PATCH] shared: always call close() from nm_close() wrapper The nm_close() wrapper should behave exactly the same as calling close() directly. This is well known, documented behavior. The only addition on top of that, should be the nm_assert() to catch double-closing. Prevously, when passing a negative file descriptor, we wouldn't properly set errno. Also, the call would not show up in strace, which it should (at least, if libc's close actually makes the syscall). I would argue, that passing a negative file descriptor is a bug already and we should never do that. Maybe we should even assert non-negative fds. I don't do that now, because I am not sufficiently confident. Anyway, the change should have not practical effect, because we shouldn't actually pass negative fds already. --- shared/nm-utils/nm-macros-internal.h | 11 +++++------ 1 file changed, 5 insertions(+), 6 deletions(-) diff --git a/shared/nm-utils/nm-macros-internal.h b/shared/nm-utils/nm-macros-internal.h index 2d62ba7d97..f4dca4b37a 100644 --- a/shared/nm-utils/nm-macros-internal.h +++ b/shared/nm-utils/nm-macros-internal.h @@ -1246,12 +1246,11 @@ nm_steal_fd (int *p_fd) static inline int nm_close (int fd) { - if (fd >= 0) { - if (close (fd) == 0) - return 0; - nm_assert (errno != EBADF); - } - return -1; + int r; + + r = close (fd); + nm_assert (r != -1 || fd < 0 || errno != EBADF); + return r; } #endif /* __NM_MACROS_INTERNAL_H__ */