public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
* FD_{SET,ISSET,CLR} macros from sys/select.h triggerring gcc's -Wsign-conversion warnings
@ 2020-08-02 18:08 Petr Skocik
  2020-08-03 10:45 ` Corinna Vinschen
  0 siblings, 1 reply; 4+ messages in thread
From: Petr Skocik @ 2020-08-02 18:08 UTC (permalink / raw)
  To: cygwin

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


^ permalink raw reply	[flat|nested] 4+ messages in thread

end of thread, other threads:[~2020-08-03 14:29 UTC | newest]

Thread overview: 4+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-08-02 18:08 FD_{SET,ISSET,CLR} macros from sys/select.h triggerring gcc's -Wsign-conversion warnings Petr Skocik
2020-08-03 10:45 ` Corinna Vinschen
2020-08-03 13:27   ` Petr Skocik
2020-08-03 14:29     ` Corinna Vinschen

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).