FD_{SET,ISSET,CLR} macros from sys/select.h triggerring gcc's -Wsign-conversion warnings
Corinna Vinschen
corinna-cygwin@cygwin.com
Mon Aug 3 10:45:52 GMT 2020
Hi Petr,
On Aug 2 20:08, Petr Skocik via Cygwin wrote:
> Example:
>
> #include <sys/select.h>
> void f(int X)
> {
> fd_set set;
> FD_ZERO(&set);
> FD_SET(X,&set);
> FD_CLR(X+1,&set);
> (void)FD_ISSET(X+2,&set);
> }
>
> causes
>
> fds.c:7:2: warning: conversion to ‘long unsigned int’ from ‘int’ may
> change the sign of the result [-Wsign-conversion]
> FD_SET(X,&set);
> ^~~~~~
> [...]
> on gcc with -Wconversion -Wsign-conversion.
>
> The problem is caused by the following macros:
>
> # define NFDBITS (sizeof (fd_mask) * 8) /* bits per mask */
> # define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1L <<
> ((n) % NFDBITS)))
> # define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1L <<
> ((n) % NFDBITS)))
> # define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1L <<
> ((n) % NFDBITS)))
>
> int-casting the sizeof and using 1UL instead of 1L fixes the problem:
Thanks. I pushed a patch which will show up in the next Cygwin release:
https://sourceware.org/git/?p=newlib-cygwin.git;a=commitdiff;h=5717262b8ecf
Funny enough, while the GLibc version of sys/select.h does not generate
these warnings, the same problem still exists in the upstream FreeBSD
code.
For testing I uploaded a new developer snapshot to
https://cygwin.com/snapshots/ You only need to fetch the new
sys/select.h file.
Thanks,
Corinna
--
Corinna Vinschen
Cygwin Maintainer
More information about the Cygwin
mailing list