Blame SOURCES/net-snmp-5.7.2-large-fdset.patch

9a6c41
diff -urNp old/include/net-snmp/library/large_fd_set.h new/include/net-snmp/library/large_fd_set.h
9a6c41
--- old/include/net-snmp/library/large_fd_set.h	2012-10-10 00:28:58.000000000 +0200
9a6c41
+++ new/include/net-snmp/library/large_fd_set.h	2017-04-04 14:36:27.307180033 +0200
9a6c41
@@ -55,8 +55,9 @@ extern "C" {
9a6c41
  * Number of bytes needed to store a number of file descriptors as a
9a6c41
  * struct fd_set.
9a6c41
  */
9a6c41
-#define NETSNMP_FD_SET_BYTES(setsize)                           \
9a6c41
-    (sizeof(fd_set) + ((setsize) - FD_SETSIZE) * sizeof(SOCKET))
9a6c41
+#define NETSNMP_FD_SET_BYTES(setsize)                                   \
9a6c41
+    (sizeof(fd_set) + ((setsize) > FD_SETSIZE ?                         \
9a6c41
+                       ((setsize) - FD_SETSIZE) * sizeof(SOCKET) : 0))
9a6c41
 
9a6c41
 /** Remove all sockets from the set *fdset. */
9a6c41
 #define NETSNMP_LARGE_FD_ZERO(fdset) \
9a6c41
@@ -91,9 +92,10 @@ int    netsnmp_large_fd_is_set(SOCKET fd
9a6c41
  * Number of bytes needed to store a number of file descriptors as a
9a6c41
  * struct fd_set.
9a6c41
  */
9a6c41
-#define NETSNMP_FD_SET_BYTES(setsize)                                   \
9a6c41
-    (sizeof(fd_set) + NETSNMP_FD_SET_ELEM_COUNT((setsize) - FD_SETSIZE) \
9a6c41
-     * NETSNMP_FD_MASK_SIZE)
9a6c41
+#define NETSNMP_FD_SET_BYTES(setsize)                                    \
9a6c41
+    (sizeof(fd_set) + ((setsize) > FD_SETSIZE ?                          \
9a6c41
+                       NETSNMP_FD_SET_ELEM_COUNT((setsize) - FD_SETSIZE) \
9a6c41
+                       * NETSNMP_FD_MASK_SIZE : 0))
9a6c41
 
9a6c41
 /** Remove all file descriptors from the set *fdset. */
9a6c41
 #define NETSNMP_LARGE_FD_ZERO(fdset)                            \
9a6c41
diff -urNp old/snmplib/large_fd_set.c new/snmplib/large_fd_set.c
9a6c41
--- old/snmplib/large_fd_set.c	2012-10-10 00:28:58.000000000 +0200
9a6c41
+++ new/snmplib/large_fd_set.c	2017-04-04 15:03:20.580810774 +0200
9a6c41
@@ -21,6 +21,10 @@
9a6c41
 
9a6c41
 #if !defined(cygwin) && defined(HAVE_WINSOCK_H)
9a6c41
 
9a6c41
+#define LFD_SET(n, p)    FD_SET(n, p)
9a6c41
+#define LFD_CLR(n, p)    FD_CLR(n, p)
9a6c41
+#define LFD_ISSET(n, p)  FD_ISSET(n, p)
9a6c41
+
9a6c41
 void
9a6c41
 netsnmp_large_fd_setfd(SOCKET fd, netsnmp_large_fd_set * fdset)
9a6c41
 {
9a6c41
@@ -28,18 +32,18 @@ netsnmp_large_fd_setfd(SOCKET fd, netsnm
9a6c41
 
9a6c41
     netsnmp_assert(fd != INVALID_SOCKET);
9a6c41
 
9a6c41
-    if (fdset->lfs_set.fd_count == fdset->lfs_setsize)
9a6c41
+    if (fdset->lfs_setptr->fd_count == fdset->lfs_setsize)
9a6c41
         netsnmp_large_fd_set_resize(fdset, 2 * (fdset->lfs_setsize + 1));
9a6c41
 
9a6c41
-    for (i = 0; i < fdset->lfs_set.fd_count; i++) {
9a6c41
-        if (fdset->lfs_set.fd_array[i] == (SOCKET) (fd))
9a6c41
+    for (i = 0; i < fdset->lfs_setptr->fd_count; i++) {
9a6c41
+        if (fdset->lfs_setptr->fd_array[i] == fd)
9a6c41
             break;
9a6c41
     }
9a6c41
 
9a6c41
-    if (i == fdset->lfs_set.fd_count
9a6c41
-        && fdset->lfs_set.fd_count < fdset->lfs_setsize) {
9a6c41
-        fdset->lfs_set.fd_count++;
9a6c41
-        fdset->lfs_set.fd_array[i] = fd;
9a6c41
+    if (i == fdset->lfs_setptr->fd_count &&
9a6c41
+        fdset->lfs_setptr->fd_count < fdset->lfs_setsize) {
9a6c41
+        fdset->lfs_setptr->fd_count++;
9a6c41
+        fdset->lfs_setptr->fd_array[i] = fd;
9a6c41
     }
9a6c41
 }
9a6c41
 
9a6c41
@@ -50,14 +54,14 @@ netsnmp_large_fd_clr(SOCKET fd, netsnmp_
9a6c41
 
9a6c41
     netsnmp_assert(fd != INVALID_SOCKET);
9a6c41
 
9a6c41
-    for (i = 0; i < fdset->lfs_set.fd_count; i++) {
9a6c41
-        if (fdset->lfs_set.fd_array[i] == fd) {
9a6c41
-            while (i < fdset->lfs_set.fd_count - 1) {
9a6c41
-                fdset->lfs_set.fd_array[i] =
9a6c41
-                    fdset->lfs_set.fd_array[i + 1];
9a6c41
+    for (i = 0; i < fdset->lfs_setptr->fd_count; i++) {
9a6c41
+        if (fdset->lfs_setptr->fd_array[i] == fd) {
9a6c41
+            while (i < fdset->lfs_setptr->fd_count - 1) {
9a6c41
+                fdset->lfs_setptr->fd_array[i] =
9a6c41
+                    fdset->lfs_setptr->fd_array[i + 1];
9a6c41
                 i++;
9a6c41
             }
9a6c41
-            fdset->lfs_set.fd_count--;
9a6c41
+            fdset->lfs_setptr->fd_count--;
9a6c41
             break;
9a6c41
         }
9a6c41
     }
9a6c41
@@ -70,8 +74,8 @@ netsnmp_large_fd_is_set(SOCKET fd, netsn
9a6c41
 
9a6c41
     netsnmp_assert(fd != INVALID_SOCKET);
9a6c41
 
9a6c41
-    for (i = 0; i < fdset->lfs_set.fd_count; i++) {
9a6c41
-        if (fdset->lfs_set.fd_array[i] == fd)
9a6c41
+    for (i = 0; i < fdset->lfs_setptr->fd_count; i++) {
9a6c41
+        if (fdset->lfs_setptr->fd_array[i] == fd)
9a6c41
             return 1;
9a6c41
     }
9a6c41
     return 0;
9a6c41
@@ -79,6 +83,43 @@ netsnmp_large_fd_is_set(SOCKET fd, netsn
9a6c41
 
9a6c41
 #else
9a6c41
 
9a6c41
+ /*
9a6c41
+ * Recent versions of glibc trigger abort() if FD_SET(), FD_CLR() or
9a6c41
+ * FD_ISSET() is invoked with n >= FD_SETSIZE. Hence these replacement
9a6c41
+ * functions. However, since NFDBITS != 8 * sizeof(fd_set.fds_bits[0]) for at
9a6c41
+ * least HP-UX on ia64 and since that combination uses big endian, use the
9a6c41
+ * macros from <sys/select.h> on such systems.
9a6c41
+ */
9a6c41
+NETSNMP_STATIC_INLINE void LFD_SET(unsigned n, fd_set *p)
9a6c41
+{
9a6c41
+    enum { nfdbits = 8 * sizeof(p->fds_bits[0]) };
9a6c41
+
9a6c41
+    if (nfdbits == NFDBITS)
9a6c41
+        p->fds_bits[n / nfdbits] |= (1ULL << (n % nfdbits));
9a6c41
+    else
9a6c41
+        FD_SET(n, p);
9a6c41
+}
9a6c41
+
9a6c41
+NETSNMP_STATIC_INLINE void LFD_CLR(unsigned n, fd_set *p)
9a6c41
+{
9a6c41
+    enum { nfdbits = 8 * sizeof(p->fds_bits[0]) };
9a6c41
+
9a6c41
+    if (nfdbits == NFDBITS)
9a6c41
+        p->fds_bits[n / nfdbits] &= ~(1ULL << (n % nfdbits));
9a6c41
+    else
9a6c41
+        FD_CLR(n, p);
9a6c41
+}
9a6c41
+
9a6c41
+NETSNMP_STATIC_INLINE unsigned LFD_ISSET(unsigned n, const fd_set *p)
9a6c41
+{
9a6c41
+    enum { nfdbits = 8 * sizeof(p->fds_bits[0]) };
9a6c41
+
9a6c41
+    if (nfdbits == NFDBITS)
9a6c41
+        return (p->fds_bits[n / nfdbits] & (1ULL << (n % nfdbits))) != 0;
9a6c41
+    else
9a6c41
+        return FD_ISSET(n, p) != 0;
9a6c41
+}
9a6c41
+
9a6c41
 void
9a6c41
 netsnmp_large_fd_setfd(int fd, netsnmp_large_fd_set * fdset)
9a6c41
 {
9a6c41
@@ -87,7 +128,7 @@ netsnmp_large_fd_setfd(int fd, netsnmp_l
9a6c41
     while (fd >= (int)fdset->lfs_setsize)
9a6c41
         netsnmp_large_fd_set_resize(fdset, 2 * (fdset->lfs_setsize + 1));
9a6c41
 
9a6c41
-    FD_SET(fd, fdset->lfs_setptr);
9a6c41
+    LFD_SET(fd, fdset->lfs_setptr);
9a6c41
 }
9a6c41
 
9a6c41
 void
9a6c41
@@ -96,7 +137,7 @@ netsnmp_large_fd_clr(int fd, netsnmp_lar
9a6c41
     netsnmp_assert(fd >= 0);
9a6c41
 
9a6c41
     if ((unsigned)fd < fdset->lfs_setsize)
9a6c41
-        FD_CLR(fd, fdset->lfs_setptr);
9a6c41
+        LFD_CLR(fd, fdset->lfs_setptr);
9a6c41
 }
9a6c41
 
9a6c41
 int
9a6c41
@@ -104,7 +145,7 @@ netsnmp_large_fd_is_set(int fd, netsnmp_
9a6c41
 {
9a6c41
     netsnmp_assert(fd >= 0);
9a6c41
 
9a6c41
-    return (unsigned)fd < fdset->lfs_setsize && FD_ISSET(fd, fdset->lfs_setptr);
9a6c41
+    return ((unsigned)fd < fdset->lfs_setsize && LFD_ISSET(fd, fdset->lfs_setptr));
9a6c41
 }
9a6c41
 
9a6c41
 #endif
9a6c41
@@ -174,22 +215,24 @@ netsnmp_large_fd_set_resize(netsnmp_larg
9a6c41
     }
9a6c41
 
9a6c41
 #if defined(cygwin) || !defined(HAVE_WINSOCK_H)
9a6c41
-    {
9a6c41
+    /*
9a6c41
+     * Unix: when enlarging, clear the file descriptors defined in the
9a6c41
+     * resized *fdset but that were not defined in the original *fdset.
9a6c41
+     */
9a6c41
+    if ( fdset->lfs_setsize == 0 && setsize == FD_SETSIZE ) {
9a6c41
+        /* In this case we can use the OS's FD_ZERO */
9a6c41
+        FD_ZERO(fdset->lfs_setptr);
9a6c41
+    } else {
9a6c41
         int             i;
9a6c41
-
9a6c41
-        /*
9a6c41
-         * Unix: when enlarging, clear the file descriptors defined in the
9a6c41
-         * resized *fdset but that were not defined in the original *fdset.
9a6c41
-         */
9a6c41
         for (i = fdset->lfs_setsize; i < setsize; i++)
9a6c41
-            FD_CLR(i, fdset->lfs_setptr);
9a6c41
+            LFD_CLR(i, fdset->lfs_setptr);
9a6c41
     }
9a6c41
 #endif
9a6c41
 
9a6c41
     fdset->lfs_setsize = setsize;
9a6c41
 #if !defined(cygwin) && defined(HAVE_WINSOCK_H)
9a6c41
-    if (setsize < fdset->lfs_set.fd_count)
9a6c41
-        fdset->lfs_set.fd_count = setsize;
9a6c41
+    if (setsize < fdset->lfs_setptr->fd_count)
9a6c41
+        fdset->lfs_setptr->fd_count = setsize;
9a6c41
 #endif
9a6c41
 success:
9a6c41
     return 1;
9a6c41
@@ -197,7 +240,7 @@ success:
9a6c41
 out_of_mem:
9a6c41
     fdset->lfs_setsize = 0;
9a6c41
 #if !defined(cygwin) && defined(HAVE_WINSOCK_H)
9a6c41
-    fdset->lfs_set.fd_count = 0;
9a6c41
+    fdset->lfs_setptr->fd_count = 0;
9a6c41
 #endif
9a6c41
     return 0;
9a6c41
 }