Quellcode durchsuchen

2010-04-25 Tatsuhiro Tsujikawa <t-tujikawa@users.sourceforge.net>

	Print warning when trying to add file descriptor >= FD_SET or < 0
	to fd_set for other than MinGW32 build. For MinGW32 build, print
	warning when trying to add file descriptor to fd_set when it
	already contains FD_SET file descriptors.
	* src/SelectEventPoll.cc
	* src/SocketCore.cc
Tatsuhiro Tsujikawa vor 15 Jahren
Ursprung
Commit
a53ee58746
3 geänderte Dateien mit 46 neuen und 0 gelöschten Zeilen
  1. 9 0
      ChangeLog
  2. 23 0
      src/SelectEventPoll.cc
  3. 14 0
      src/SocketCore.cc

+ 9 - 0
ChangeLog

@@ -1,3 +1,12 @@
+2010-04-25  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
+
+	Print warning when trying to add file descriptor >= FD_SET or < 0
+	to fd_set for other than MinGW32 build. For MinGW32 build, print
+	warning when trying to add file descriptor to fd_set when it
+	already contains FD_SET file descriptors.
+	* src/SelectEventPoll.cc
+	* src/SocketCore.cc
+
 2010-04-25  Tatsuhiro Tsujikawa  <t-tujikawa@users.sourceforge.net>
 
 	Updated doc for --event-poll option.

+ 23 - 0
src/SelectEventPoll.cc

@@ -224,6 +224,16 @@ void SelectEventPoll::poll(const struct timeval& tv)
 #endif // ENABLE_ASYNC_DNS
 }
 
+#ifdef __MINGW32__
+static void checkFdCountMingw(const fd_set& fdset, Logger* logger)
+{
+  if(fdset.fd_count >= FD_SETSIZE) {
+    logger->warn("The number of file descriptor exceeded FD_SETSIZE. "
+                 "Download may slow down or fail.");
+  }
+}
+#endif // __MINGW32__
+
 void SelectEventPoll::updateFdSet()
 {
 #ifdef __MINGW32__
@@ -236,11 +246,24 @@ void SelectEventPoll::updateFdSet()
   for(std::deque<SharedHandle<SocketEntry> >::const_iterator i =
         _socketEntries.begin(), eoi = _socketEntries.end(); i != eoi; ++i) {
     sock_t fd = (*i)->getSocket();
+#ifndef __MINGW32__
+    if(fd < 0 || FD_SETSIZE <= fd) {
+      _logger->warn("Detected file descriptor >= FD_SETSIZE or < 0. "
+                    "Download may slow down or fail.");
+      continue;
+    }
+#endif // !__MINGW32__
     int events = (*i)->getEvents();
     if(events&EventPoll::EVENT_READ) {
+#ifdef __MINGW32__
+      checkFdCountMingw(_rfdset, _logger);
+#endif // __MINGW32__
       FD_SET(fd, &_rfdset);
     }
     if(events&EventPoll::EVENT_WRITE) {
+#ifdef __MINGW32__
+      checkFdCountMingw(_wfdset, _logger);
+#endif // __MINGW32__
       FD_SET(fd, &_wfdset);
     }
     if(_fdmax < fd) {

+ 14 - 0
src/SocketCore.cc

@@ -546,6 +546,14 @@ void SocketCore::closeConnection()
 #endif // HAVE_LIBGNUTLS
 }
 
+#ifndef __MINGW32__
+# define CHECK_FD(fd)                                                   \
+  if(fd < 0 || FD_SETSIZE <= fd) {                                      \
+    _logger->warn("Detected file descriptor >= FD_SETSIZE or < 0. "     \
+                  "Download may slow down or fail.");                   \
+    return false;                                                       \
+  }
+#endif // !__MINGW32__
 
 bool SocketCore::isWritable(time_t timeout)
 {
@@ -564,6 +572,9 @@ bool SocketCore::isWritable(time_t timeout)
       (StringFormat(EX_SOCKET_CHECK_WRITABLE, errorMsg()).str());
   }
 #else // !HAVE_POLL
+# ifndef __MINGW32__
+  CHECK_FD(sockfd);
+# endif // !__MINGW32__
   fd_set fds;
   FD_ZERO(&fds);
   FD_SET(sockfd, &fds);
@@ -611,6 +622,9 @@ bool SocketCore::isReadable(time_t timeout)
       (StringFormat(EX_SOCKET_CHECK_READABLE, errorMsg()).str());
   }
 #else // !HAVE_POLL
+# ifndef __MINGW32__
+  CHECK_FD(sockfd);
+# endif // !__MINGW32__
   fd_set fds;
   FD_ZERO(&fds);
   FD_SET(sockfd, &fds);