Subject: lib/17746: FD_ZERO, sysconf(_SC_OPEN_MAX) and select() don't work together
To: None <gnats-bugs@gnats.netbsd.org>
From: None <ChristianBiere@gmx.de>
List: netbsd-bugs
Date: 07/28/2002 13:46:27
>Number:         17746
>Category:       lib
>Synopsis:       FD_ZERO, sysconf(_SC_OPEN_MAX) and select() don't work together
>Confidential:   no
>Severity:       serious
>Priority:       medium
>Responsible:    lib-bug-people
>State:          open
>Class:          sw-bug
>Submitter-Id:   net
>Arrival-Date:   Sun Jul 28 13:47:00 PDT 2002
>Closed-Date:
>Last-Modified:
>Originator:     Christian Biere
>Release:        NetBSD 1.6D
>Organization:
>Environment:
System: NetBSD localhost 1.6D NetBSD 1.6D (DURON2) #0: Sat Jul 27 08:52:59 CEST
2002 root@localhost:/usr/src/sys/arch/i386/compile/DURON2 i386
Architecture: i386
Machine: i386

>Description:
Look at these code from /usr/include/sys/types.h:

#ifndef FD_SETSIZE
#define FD_SETSIZE      256
#endif

typedef struct fd_set {
        __fd_mask       fds_bits[__howmany(FD_SETSIZE, __NFDBITS)];
} fd_set;

#define FD_ZERO(p)      (void)memset((p), 0, sizeof(*(p)))

This means an application must not use the result of 
sysconf(_SC_OPEN_MAX) and use this as the first parameter for
select() if this value is higher than FD_SETSIZE. As the admin
may always increase this limit (kern.maxfiles, 
proc.curproc.rlimit.descriptors.soft, 
proc.curproc.rlimit.descriptors.hard) FD_SETSIZE and _SC_OPEN_MAX
can never be in sync.

>How-To-Repeat:
Look at e.g. pkgsrc/x11/aterm and what it does with select() in
command.c.
>Fix:
I don't know whether this can be fixed at all but it should be
documented.
>Release-Note:
>Audit-Trail:
>Unformatted: