Impossible to use GCC with -Wconversion and standard socket macros?
ProgrammingThis forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.
Notices
Welcome to LinuxQuestions.org, a friendly and active Linux Community.
You are currently viewing LQ as a guest. By joining our community you will have the ability to post topics, receive our newsletter, use the advanced search, subscribe to threads and access many other special features. Registration is quick, simple and absolutely free. Join our community today!
Note that registered members see fewer ads, and ContentLink is completely disabled once you log in.
If you have any problems with the registration process or your account login, please contact us. If you need to reset your password, click here.
Having a problem logging in? Please visit this page to clear all LQ-related cookies.
Get a virtual cloud desktop with the Linux distro that you want in less than five minutes with Shells! With over 10 pre-installed distros to choose from, the worry-free installation life is here! Whether you are a digital nomad or just looking for flexibility, Shells can put your Linux machine on the device that you want to use.
Exclusive for LQ members, get up to 45% off per month. Click here for more info.
Impossible to use GCC with -Wconversion and standard socket macros?
When coding I like to enable many warnings in GCC, for example Wall, -Wextra, -Wconversion etc.
However, i discovered one problem with standard macros which generates warning when -Wconversion is enabled and there doesn't seem any way around this except rewriting the standard macros.
Standard socket code often have variations of these lines
However, this can not be compiled cleanly with -Wconversion and no user level type casting can eliminate these warnings (as far as I can understand having looked at the macros)
What have other people done in this situation ?
P.S Using #pragma GCC diagnostic ignored "-Wconversion" doesn't seem to disable these warnings.
However, this can not be compiled cleanly with -Wconversion and no user level type casting can eliminate these warnings (as far as I can understand having looked at the macros)
What version of gcc are you using? The following compiles just fine for me without any warnings:
Code:
$ cat test.c
#include <sys/select.h>
int main(void)
{
fd_set fds;
int fd = 0;
FD_ZERO(&fds);
FD_SET(fd, &fds);
return 0;
}
$ make test
cc -std=gnu99 -Wall -Wextra -Werror -g -Waggregate-return -Wall -Wbad-function-cast -Wcast-qual -Wdisabled-optimization -Werror -Wfloat-equal -Winline -Wlong-long -Wmissing-declarations -Wmissing-noreturn -Wmissing-prototypes -Wnested-externs -Wredundant-decls -Wshadow -Wsign-compare -Wstrict-prototypes -Wundef -Wwrite-strings -Wconversion test.c -o test
$ echo $?
0
$ gcc --version
gcc (Ubuntu 4.4.3-4ubuntu5) 4.4.3
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
Interesting. If I compile this code (under the name wtst.c) I get:
Code:
gcc -Werror -Wconversion wtst.c
cc1: warnings being treated as errors
wtst.c: In function ‘main’:
wtst.c:8: error: conversion to ‘unsigned int’ from ‘int’ may change the sign of the result
wtst.c:8: error: conversion to ‘unsigned int’ from ‘int’ may change the sign of the result
the version of gcc I use is
Code:
gcc --version
gcc (SUSE Linux) 4.4.1 [gcc-4_4-branch revision 150839]
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
If I remove the (int) casts I get the same errors you get - however, replacing "fd" with "((unsigned int)fd)" gets rid of the errors, so using FD_SET((unsigned int)fd, &fds) should solve your problem.
As far as can tell the core problem is thát sizeof() has return type size_t (which usually is an unsigned type, and most definitely on X86 arch) and this gets automatically downcasted to an int by gcc in the expressions above and this is the two warnings.
The reason the first warning goes away is that in this case (as can be seen from the expression above) the first conversion is fine but in the second the macro has an explicit (int) cast which is the problematic part.
So, without changing the macro manually this cannot be fixed. I consider this a bug within the gnu clib but I might be overlooking something.
The reason the first warning goes away is that in this case (as can be seen from the expression above) the first conversion is fine but in the second the macro has an explicit (int) cast which is the problematic part.
Yeah, it seems weird that they'd give one occurrence of fd an explicit cast but not the other...
Either way, yeah, I can't see any way to get over it cleanly. Not too sure how you'd go about reporting that. I know if you run a glibc library (e.g. run "/lib/libc.so.6" at a shell) it'll print version info that'll be important when reporting it. Maybe you're best reporting it to the SUSE people...???
In relation to this I also discovered another signed:ness optimization bug in gcc.
The following (legitimate) code will also generate a warning when both -O2 and -Wconversion is enabled but not when only -Wconversion is enabled. The optimization is to clever for its own good
Code:
#include <arpa/inet.h>
// Nonsense code to illustrate problem
int main(void) {
uint16_t portnbr=0;
uint16_t n_portnbr = htons(portnbr);
return n_portnbr;
}
With optimization
Code:
$> gcc -std=gnu99 -Wconversion -Werror -O2 -c tsthtons.c
cc1: warnings being treated as errors
tsthtons.c: In function ‘main’:
tsthtons.c:6: error: conversion to ‘short unsigned int’ from ‘int’ may alter its value
gcc --version
gcc (SUSE Linux) 4.4.1 [gcc-4_4-branch revision 150839]
Copyright (C) 2009 Free Software Foundation, Inc.
This is free software; see the source for copying conditions. There is NO
warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
To me it seems like the gcc -Wconversion flag (while in principle good and can probably help discover some subtle once-in-a-blue-moon bugs) has not really been through enough tests in real life code. Looking at some various large code bases this flag is never used. (Probably because of the huge number of false positives that people consider irritating, like the pattern "int n=strnlen(buffer,MAX_BUFFLEN);" that will generate a warning that is technically correct but on code that generally is agreed on as acceptable.
In most places I worked there has in general been a rule that all code should be "silent" with maximum warnings enabled and that any warning will break the build (an the culprit submitting breaking code buys everyone beer in the local)
LinuxQuestions.org is looking for people interested in writing
Editorials, Articles, Reviews, and more. If you'd like to contribute
content, let us know.