Range check winsock2's socket() return value
[xapian.git] / xapian-core / common / safesysselect.h
blob92de5768db53473041e22272fa601ec935489b25
1 /** @file safesysselect.h
2 * @brief #include <sys/select.h> with portability workarounds.
3 */
4 /* Copyright (C) 2007,2011,2018 Olly Betts
6 * This program is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU General Public License as
8 * published by the Free Software Foundation; either version 2 of the
9 * License, or (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
21 #ifndef XAPIAN_INCLUDED_SAFESYSSELECT_H
22 #define XAPIAN_INCLUDED_SAFESYSSELECT_H
24 #ifndef PACKAGE
25 # error You must #include <config.h> before #include "safesysselect.h"
26 #endif
28 #ifndef __WIN32__
29 # ifdef HAVE_SYS_SELECT_H
30 // According to POSIX 1003.1-2001.
31 # include <sys/select.h>
32 # else
33 // According to earlier standards.
34 # include <sys/time.h>
35 # include <sys/types.h>
36 # include <unistd.h>
37 # endif
39 // On Solaris FD_SET uses memset but fails to prototype it.
40 # include <cstring>
42 #else
44 // select() and FD_SET() are defined in <winsock2.h>:
46 // The FD_SET macro expects an unsigned type (SOCKET) for the fd and passing
47 // an int can result in a warning about comparing signed and unsigned, so we
48 // add a wrapper to cast the fd argument of FD_SET to unsigned.
49 # include "safewinsock2.h"
50 inline void xapian_FD_SET_(int fd, fd_set *set) {
51 FD_SET(unsigned(fd), set);
53 # ifdef FD_SET
54 # undef FD_SET
55 # endif
56 # define FD_SET(FD,SET) xapian_FD_SET_(FD,SET)
57 #endif
59 #endif // XAPIAN_INCLUDED_SAFESYSSELECT_H