Ver Fonte

Report errors if calls to `close()` fail

rdb há 1 ano atrás
pai
commit
b49dbaca2b

+ 21 - 7
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
 }

+ 9 - 1
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

+ 10 - 2
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;

+ 3 - 1
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
 }