Build fails with strict-aliasing violations.
Brought to you by:
inlabnetworks,
obermair
I tried to compile with LTO: -flto=4 -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing
The -Werror=* flags are important to detect cases where the compiler can try to optimize based on assuming UB cannot happen, and miscompile code that has UB in it. strict-aliasing issues are always bad but LTO can make them even worse.
I got this error:
x86_64-pc-linux-gnu-gcc -march=native -fstack-protector-all -O2 -pipe -fdiagnostics-color=always -frecord-gcc-switches -U_FORTIFY_SOURCE -D_FORTIFY_SOURCE=3 -fstack-clash-protection -flto=4 -Werror=odr -Werror=lto-type-mismatch -Werror=strict-aliasing -Wformat -Werror=format-security -Werror=implicit-function-declaration -Werror=implicit-int -Werror=int-conversion -Werror=incompatible-pointer-types -Wall -Wstrict-prototypes -Wuninitialized -I. -c balance.c
balance.c: In function ‘setaddress’:
balance.c:503:9: warning: variable ‘host_string’ set but not used [-Wunused-but-set-variable]
503 | char *host_string = NULL;
| ^~~~~~~~~~~
balance.c: In function ‘setaddress_noexitonerror’:
balance.c:566:9: warning: variable ‘host_string’ set but not used [-Wunused-but-set-variable]
566 | char *host_string;
| ^~~~~~~~~~~
balance.c: In function ‘stream’:
balance.c:912:54: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
912 | uindex = hash_fold((unsigned char*) &(((struct sockaddr_in6 *) &client_address)->sin6_addr), client_address_size);
| ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
balance.c:912:34: warning: pointer targets in passing argument 1 of ‘hash_fold’ differ in signedness [-Wpointer-sign]
912 | uindex = hash_fold((unsigned char*) &(((struct sockaddr_in6 *) &client_address)->sin6_addr), client_address_size);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| unsigned char *
In file included from balance.c:114:
./balance.h:133:24: note: expected ‘char *’ but argument is of type ‘unsigned char *’
133 | unsigned int hash_fold(char *, int);
| ^~~~~~
balance.c: In function ‘main’:
balance.c:1815:30: warning: pointer targets in passing argument 1 of ‘hash_fold’ differ in signedness [-Wpointer-sign]
1815 | uindex = hash_fold((unsigned char*) &(((struct sockaddr_in6 *) &cli_addr)->sin6_addr), clilen);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
| |
| unsigned char *
./balance.h:133:24: note: expected ‘char *’ but argument is of type ‘unsigned char *’
133 | unsigned int hash_fold(char *, int);
| ^~~~~~
cc1: some warnings being treated as errors
make: *** [Makefile:32: balance.o] Error 1
Originally reported downstream: https://bugs.gentoo.org/861599
Full build log is attached.