mailing list of musl libc
 help / color / mirror / code / Atom feed
* [PATCH] select.h: Make FD_{SET,CLR,ISSET} cast their fd argument to unsigned int
@ 2018-03-14 22:42 Martin Galvan
  0 siblings, 0 replies; only message in thread
From: Martin Galvan @ 2018-03-14 22:42 UTC (permalink / raw)
  To: musl; +Cc: Martin Galvan

Currently, the FD_* macros assume their fd argument is always positive and
perform operations on it that will result in signed-to-unsigned conversions.
This will trigger gcc's -Wsign-conversion warning, which will break compilations
if -Werror is active.

This patch simply casts the fd to unsigned int. If the fd was indeed negative,
things will break as they would have before. If it was postive, the warning will
be squelched. No information should be lost on this cast either.

There may be a more elegant solution to this, though, but this seems to be good
enough IMHO.
---
 include/sys/select.h | 9 ++++++---
 1 file changed, 6 insertions(+), 3 deletions(-)

diff --git a/include/sys/select.h b/include/sys/select.h
index d34cbf10..604935e2 100644
--- a/include/sys/select.h
+++ b/include/sys/select.h
@@ -24,9 +24,12 @@ typedef struct {
 } fd_set;

 #define FD_ZERO(s) do { int __i; unsigned long *__b=(s)->fds_bits; for(__i=sizeof (fd_set)/sizeof (long); __i; __i--) *__b++=0; } while(0)
-#define FD_SET(d, s)   ((s)->fds_bits[(d)/(8*sizeof(long))] |= (1UL<<((d)%(8*sizeof(long)))))
-#define FD_CLR(d, s)   ((s)->fds_bits[(d)/(8*sizeof(long))] &= ~(1UL<<((d)%(8*sizeof(long)))))
-#define FD_ISSET(d, s) !!((s)->fds_bits[(d)/(8*sizeof(long))] & (1UL<<((d)%(8*sizeof(long)))))
+/* The casts to unsigned int are needed to avoid gcc's -Wsign-conversion warning.
+   Sign conversions shouldn't matter here because if the fd was negative there
+   was something wrong anyway, and there's not much we can do at this point. */
+#define FD_SET(d, s)   ((s)->fds_bits[(unsigned int)(d)/(8*sizeof(long))] |= (1UL<<((unsigned int)(d)%(8*sizeof(long)))))
+#define FD_CLR(d, s)   ((s)->fds_bits[(unsigned int)(d)/(8*sizeof(long))] &= ~(1UL<<((unsigned int)(d)%(8*sizeof(long)))))
+#define FD_ISSET(d, s) !!((s)->fds_bits[(unsigned int)(d)/(8*sizeof(long))] & (1UL<<((unsigned int)(d)%(8*sizeof(long)))))

 int select (int, fd_set *__restrict, fd_set *__restrict, fd_set *__restrict, struct timeval *__restrict);
 int pselect (int, fd_set *__restrict, fd_set *__restrict, fd_set *__restrict, const struct timespec *__restrict, const sigset_t *__restrict);
--
2.16.2



^ permalink raw reply	[flat|nested] only message in thread

only message in thread, other threads:[~2018-03-14 22:42 UTC | newest]

Thread overview: (only message) (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2018-03-14 22:42 [PATCH] select.h: Make FD_{SET,CLR,ISSET} cast their fd argument to unsigned int Martin Galvan

Code repositories for project(s) associated with this public inbox

	https://git.vuxu.org/mirror/musl/

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 NNTP newsgroup(s).