Bug #57562 Strict aliasing warning in vio_get_normalized_ip_string()
Submitted: 19 Oct 2010 10:58 Modified: 21 Oct 2010 15:34
Reporter: Magnus Blåudd Email Updates:
Status: Verified Impact on me:
None 
Category:MySQL Server Severity:S3 (Non-critical)
Version:5.5.7-rc OS:Any
Assigned to: CPU Architecture:Any
Triage: Triaged: D4 (Minor)

[19 Oct 2010 10:58] Magnus Blåudd
Description:
viosocket.c:1078: warning: dereferencing pointer ‘norm_addr’ does break strict-

How to repeat:
user@machine:~/mysql/5.5/vio$ ccache gcc -DHAVE_CONFIG_H -I. -I../include -I../include -I../include     -g  -DSAFE_MUTEX -Wall -Wextra -Wunused -Wwrite-strings -mtune=native -DUNIV_MUST_NOT_INLINE -DEXTRA_DEBUG -DFORCE_INIT_OF_VARS  -DSAFE_MUTEX -O3 -DUNIV_LINUX -MT viosocket.o -MD -MP -MF .deps/viosocket.Tpo -c -o viosocket.o viosocket.c
viosocket.c: In function ‘vio_get_normalized_ip_string’:
viosocket.c:1078: warning: dereferencing pointer ‘norm_addr’ does break strict-aliasing rules
viosocket.c:421: note: initialized from here

Suggested fix:
user@machine:~/mysql/7.0$ bzr diff
=== modified file 'vio/viosocket.c'
--- vio/viosocket.c	2010-05-26 09:50:01 +0000
+++ vio/viosocket.c	2010-10-19 10:54:45 +0000
@@ -856,8 +856,11 @@ my_bool vio_get_normalized_ip_string(con
                                      char *ip_string,
                                      size_t ip_string_size)
 {
-  struct sockaddr_storage norm_addr_storage;
-  struct sockaddr *norm_addr= (struct sockaddr *) &norm_addr_storage;
+  union {
+    struct sockaddr_storage norm_addr_storage;
+    struct sockaddr _avoid_strict_alias_warning;
+  } tmp_addr;
+  struct sockaddr *norm_addr= (struct sockaddr *) &tmp_addr.norm_addr_storage;
   int norm_addr_length;
   int err_code;
[19 Oct 2010 11:01] Davi Arnaut
Strict aliasing violations are being aggregated in Bug#42733. Also, the union trick only works on a couple of compilers, its not standard. The right fix is a simple cast to void...
[19 Oct 2010 11:03] Davi Arnaut
if the socket structure is being cast back to the right type.
[21 Oct 2010 15:34] Sveta Smirnova
Thank you for the report.

Verified as described.