From b49dbaca2bd3f544ef54b9a3d017eadef152de3b Mon Sep 17 00:00:00 2001 From: rdb Date: Thu, 25 Jan 2024 12:33:20 +0100 Subject: [PATCH] Report errors if calls to `close()` fail --- dtool/src/dtoolutil/filename.cxx | 28 ++++++++++++++++------ dtool/src/dtoolutil/pandaFileStreamBuf.cxx | 10 +++++++- panda/src/nativenet/socket_ip.h | 12 ++++++++-- pandatool/src/deploy-stub/deploy-stub.c | 4 +++- 4 files changed, 43 insertions(+), 11 deletions(-) diff --git a/dtool/src/dtoolutil/filename.cxx b/dtool/src/dtoolutil/filename.cxx index 7d3ce1693f..69556a5325 100644 --- a/dtool/src/dtoolutil/filename.cxx +++ b/dtool/src/dtoolutil/filename.cxx @@ -2287,7 +2287,9 @@ touch() const { perror(os_specific.c_str()); return false; } - close(fd); + if (close(fd) < 0) { + perror(os_specific.c_str()); + } return true; } perror(os_specific.c_str()); @@ -2736,7 +2738,9 @@ atomic_compare_and_exchange_contents(string &orig_contents, if (flock(fd, LOCK_EX) != 0) { #endif perror(os_specific.c_str()); - close(fd); + if (close(fd) < 0) { + perror(os_specific.c_str()); + } return false; } @@ -2748,7 +2752,9 @@ atomic_compare_and_exchange_contents(string &orig_contents, if (bytes_read < 0) { perror(os_specific.c_str()); - close(fd); + if (close(fd) < 0) { + perror(os_specific.c_str()); + } return false; } @@ -2759,7 +2765,9 @@ atomic_compare_and_exchange_contents(string &orig_contents, ssize_t bytes_written = write(fd, new_contents.data(), new_contents.size()); if (bytes_written < 0) { perror(os_specific.c_str()); - close(fd); + if (close(fd) < 0) { + perror(os_specific.c_str()); + } return false; } } @@ -2852,7 +2860,9 @@ atomic_read_contents(string &contents) const { if (flock(fd, LOCK_EX) != 0) { #endif perror(os_specific.c_str()); - close(fd); + if (close(fd) < 0) { + perror(os_specific.c_str()); + } return false; } @@ -2864,11 +2874,15 @@ atomic_read_contents(string &contents) const { if (bytes_read < 0) { perror(os_specific.c_str()); - close(fd); + if (close(fd) < 0) { + perror(os_specific.c_str()); + } return false; } - close(fd); + if (close(fd) < 0) { + perror(os_specific.c_str()); + } return true; #endif // WIN32_VC } diff --git a/dtool/src/dtoolutil/pandaFileStreamBuf.cxx b/dtool/src/dtoolutil/pandaFileStreamBuf.cxx index 1caecaf991..e5891c7158 100644 --- a/dtool/src/dtoolutil/pandaFileStreamBuf.cxx +++ b/dtool/src/dtoolutil/pandaFileStreamBuf.cxx @@ -267,7 +267,15 @@ close() { _handle = nullptr; #else if (_fd != -1) { - ::close(_fd); + if (::close(_fd) < 0) { +#ifdef NDEBUG + perror("close"); +#else + char *str = (char *)alloca(_filename.size() + 32); + sprintf(str, "close(%d \"%s\")", _fd, _filename.c_str()); + perror(str); +#endif + } } _fd = -1; #endif // _WIN32 diff --git a/panda/src/nativenet/socket_ip.h b/panda/src/nativenet/socket_ip.h index 3c03db2b3b..0589611be3 100644 --- a/panda/src/nativenet/socket_ip.h +++ b/panda/src/nativenet/socket_ip.h @@ -82,7 +82,11 @@ private: inline bool Socket_IP:: ErrorClose() { if (Active()) { - DO_CLOSE(_socket); + if (DO_CLOSE(_socket) != 0) { +#ifndef _WIN32 + perror("Socket_IP::ErrorClose"); +#endif + } } _socket = BAD_SOCKET; @@ -127,7 +131,11 @@ inline Socket_IP:: inline void Socket_IP:: Close() { if (Active()) { - DO_CLOSE(_socket); + if (DO_CLOSE(_socket) != 0) { +#ifndef _WIN32 + perror("Socket_IP::ErrorClose"); +#endif + } } _socket = BAD_SOCKET; diff --git a/pandatool/src/deploy-stub/deploy-stub.c b/pandatool/src/deploy-stub/deploy-stub.c index c9a9272b15..6fb5b1e310 100644 --- a/pandatool/src/deploy-stub/deploy-stub.c +++ b/pandatool/src/deploy-stub/deploy-stub.c @@ -359,7 +359,9 @@ static int setup_logging(const char *path, int append) { dup2(fd, 1); dup2(fd, 2); - close(fd); + if (close(fd) < 0) { + perror("setup_logging: close"); + } return 1; #endif }