FD_{SET,ISSET,CLR} macros from sys/select.h triggerring gcc's -Wsign-conversion warnings
Petr Skocik
pskocik@gmail.com
Sun Aug 2 18:08:57 GMT 2020
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);
^~~~~~
fds.c:7:2: warning: conversion to ‘long unsigned int’ from ‘int’ may
change the sign of the result [-Wsign-conversion]
fds.c:7:2: warning: conversion to ‘long unsigned int’ from ‘long int’
may change the sign of the result [-Wsign-conversion]
fds.c:8:2: warning: conversion to ‘long unsigned int’ from ‘int’ may
change the sign of the result [-Wsign-conversion]
FD_CLR(X+1,&set);
^~~~~~
fds.c:8:2: warning: conversion to ‘long unsigned int’ from ‘int’ may
change the sign of the result [-Wsign-conversion]
fds.c:8:2: warning: conversion to ‘long unsigned int’ from ‘long int’
may change the sign of the result [-Wsign-conversion]
fds.c:9:8: warning: conversion to ‘long unsigned int’ from ‘int’ may
change the sign of the result [-Wsign-conversion]
(void)FD_ISSET(X+2,&set);
^~~~~~~~
fds.c:9:8: warning: conversion to ‘long unsigned int’ from ‘int’ may
change the sign of the result [-Wsign-conversion]
fds.c:9:8: warning: conversion to ‘long unsigned int’ from ‘long int’
may change the sign of the result [-Wsign-conversion]
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:
#include <sys/select.h>
#if __CYGWIN__
//current defs # define NFDBITS (sizeof (fd_mask) * 8) /*
bits per mask */
# 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)))
#if CYGWIN_FD_REDEFS
#undef NFDBITS
#undef FD_SET
#undef FD_CLR
#undef FD_ISSET
//redefs that don't trigger gcc's -Wsign-conversion
# define NFDBITS ((int)sizeof (fd_mask) * 8) /* bits per
mask */
# define FD_SET(n, p) ((p)->fds_bits[(n)/NFDBITS] |= (1UL <<
((n) % NFDBITS)))
# define FD_CLR(n, p) ((p)->fds_bits[(n)/NFDBITS] &= ~(1UL <<
((n) % NFDBITS)))
# define FD_ISSET(n, p) ((p)->fds_bits[(n)/NFDBITS] & (1UL <<
((n) % NFDBITS)))
#endif
#endif
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);
}
Regards,
Petr Skocik
More information about the Cygwin
mailing list