From 71f7875c954a63d6ee5fad97ff97c551015f8a47 Mon Sep 17 00:00:00 2001 From: Christian Grothoff Date: Thu, 8 Oct 2009 08:52:48 +0000 Subject: [PATCH] check for FD_SETSIZE --- src/util/network.c | 39 +++++++++++++++++++++++++++++++-------- 1 file changed, 31 insertions(+), 8 deletions(-) diff --git a/src/util/network.c b/src/util/network.c index 91d4066b2..8bdace580 100644 --- a/src/util/network.c +++ b/src/util/network.c @@ -31,6 +31,10 @@ #define DEBUG_SOCK GNUNET_NO +#ifndef INVALID_SOCKET +#define INVALID_SOCKET -1 +#endif + struct GNUNET_NETWORK_Handle { int fd; @@ -68,9 +72,22 @@ GNUNET_NETWORK_socket_accept (const struct GNUNET_NETWORK_Handle *desc, ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle)); ret->fd = accept (desc->fd, address, address_len); + if (ret->fd == INVALID_SOCKET) + { #ifdef MINGW - if (INVALID_SOCKET == ret->fd) - SetErrnoFromWinsockError (WSAGetLastError ()); + SetErrnoFromWinsockError (WSAGetLastError ()); +#endif + GNUNET_free (ret); + return NULL; + } +#ifndef MINGW + if (ret->fd >= FD_SETSIZE) + { + close (desc->fd); + GNUNET_free (ret); + errno = EMFILE; + return NULL; + } #endif return ret; } @@ -347,17 +364,23 @@ GNUNET_NETWORK_socket_socket (int domain, int type, int protocol) ret = GNUNET_malloc (sizeof (struct GNUNET_NETWORK_Handle)); ret->fd = socket (domain, type, protocol); -#ifdef MINGW if (INVALID_SOCKET == ret->fd) - SetErrnoFromWinsockError (WSAGetLastError ()); + { +#ifdef MINGW + SetErrnoFromWinsockError (WSAGetLastError ()); #endif - - if (ret->fd < 0) + GNUNET_free (ret); + return NULL; + } +#ifndef MINGW + if (ret->fd >= FD_SETSIZE) { + close (ret->fd); GNUNET_free (ret); - ret = NULL; + errno = EMFILE; + return NULL; } - +#endif return ret; } -- 2.25.1