public inbox for cygwin@cygwin.com
 help / color / mirror / Atom feed
From: Petr Skocik <pskocik@gmail.com>
To: cygwin@cygwin.com
Subject: FD_{SET,ISSET,CLR} macros from sys/select.h triggerring gcc's -Wsign-conversion warnings
Date: Sun, 2 Aug 2020 20:08:57 +0200	[thread overview]
Message-ID: <1f188b7e-6dc4-73af-e458-013760210469@gmail.com> (raw)

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


             reply	other threads:[~2020-08-02 18:09 UTC|newest]

Thread overview: 4+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-08-02 18:08 Petr Skocik [this message]
2020-08-03 10:45 ` Corinna Vinschen
2020-08-03 13:27   ` Petr Skocik
2020-08-03 14:29     ` Corinna Vinschen

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1f188b7e-6dc4-73af-e458-013760210469@gmail.com \
    --to=pskocik@gmail.com \
    --cc=cygwin@cygwin.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).