Follow @Openwall on Twitter for new release announcements and other news
[<prev] [next>] [day] [month] [year] [list]
Date: Wed, 14 Mar 2018 19:42:35 -0300
From: Martin Galvan <omgalvan.86@...il.com>
To: musl@...ts.openwall.com
Cc: Martin Galvan <omgalvan.86@...il.com>
Subject: [PATCH] select.h: Make FD_{SET,CLR,ISSET} cast their fd argument to unsigned int

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

Powered by blists - more mailing lists

Confused about mailing lists and their use? Read about mailing lists on Wikipedia and check out these guidelines on proper formatting of your messages.