This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
[PATCH v3] fix warnings in sys/select.h fortification with -Wsign-conversion
- From: Mike Frysinger <vapier at gentoo dot org>
- To: libc-alpha at sourceware dot org
- Date: Fri, 8 Jun 2012 23:50:21 -0400
- Subject: [PATCH v3] fix warnings in sys/select.h fortification with -Wsign-conversion
- References: <1339195817-14531-1-git-send-email-vapier@gentoo.org>
If we build code such as:
#include <sys/select.h>
int main(int argc, char *argv[])
{
int fd = argc;
fd_set fds;
FD_ZERO(&fds);
FD_SET(fd, &fds);
return argv[0][fd];
}
with warning flags such as:
$ gcc test.c -c -Wsign-conversion -O2 -Werror -D_FORTIFY_SOURCE=2
the build fails like so:
error: conversion to 'long unsigned int' from 'int' may change
the sign of the result [-Werror=sign-conversion]
This is because the fortification code is casting the signed fd up to
an unsigned value. If the fd is actually negative (causing the unsigned
value to cast to a large number), the following check in the code will
catch it and warn properly.
So add an explicit cast to avoid the warning.
Signed-off-by: Mike Frysinger <vapier@gentoo.org>
2012-06-08 Mike Frysinger <vapier@gentoo.org>
[BZ #14210]
* misc/bits/select2.h (__FD_ELT): Cast D to unsigned long int.
---
v3
- add code comment and update changelog
misc/bits/select2.h | 3 ++-
1 file changed, 2 insertions(+), 1 deletion(-)
diff --git a/misc/bits/select2.h b/misc/bits/select2.h
index 9679925..1e848d8 100644
--- a/misc/bits/select2.h
+++ b/misc/bits/select2.h
@@ -27,7 +27,8 @@ extern unsigned long int __fdelt_warn (unsigned long int __d)
#undef __FD_ELT
#define __FD_ELT(d) \
__extension__ \
- ({ unsigned long int __d = (d); \
+ ({ /* Need to cast as "d" is an int fd and can cause signed warnings. */ \
+ unsigned long int __d = (unsigned long int) (d); \
(__builtin_constant_p (__d) \
? (__d >= __FD_SETSIZE \
? __fdelt_warn (__d) : (__d / __NFDBITS)) \
--
1.7.9.7