miniz icon indicating copy to clipboard operation
miniz copied to clipboard

Compiler warnings on strict aliasing

Open GoogleCodeExporter opened this issue 9 years ago • 7 comments

What steps will reproduce the problem?
1. gcc -Wall -Werror -O2 miniz.c
2.
3.

What is the expected output? What do you see instead?
Expected output: Compiler errors due to no main().
Actual output:
Compiler errors due to type punning.
miniz.c: In function ‘tdefl_find_match’:
miniz.c:2270:3: error: dereferencing type-punned pointer will break 
strict-aliasing rules [-Werror=strict-aliasing]
miniz.c:2282:7: error: dereferencing type-punned pointer will break 
strict-aliasing rules [-Werror=strict-aliasing]
miniz.c:2282:7: error: dereferencing type-punned pointer will break 
strict-aliasing rules [-Werror=strict-aliasing]
miniz.c:2282:7: error: dereferencing type-punned pointer will break 
strict-aliasing rules [-Werror=strict-aliasing]
miniz.c:2294:7: error: dereferencing type-punned pointer will break 
strict-aliasing rules [-Werror=strict-aliasing]
miniz.c: In function ‘tdefl_compress_fast’:
miniz.c:2367:7: error: dereferencing type-punned pointer will break 
strict-aliasing rules [-Werror=strict-aliasing]

What version of the product are you using? On what operating system?
9.1.15, all MINIZ_NO_* enabled. Linux Mint 15, AMD64, GCC 4.7.3-1ubuntu1.

Please provide any additional information below.
Disabling warnings, optimization, or both, makes this go away.

Original issue reported on code.google.com by [email protected] on 24 Nov 2013 at 5:02

GoogleCodeExporter avatar May 21 '15 03:05 GoogleCodeExporter

I'll take a look at this in my latest devel version and see what I can do.

With gcc/clang I *always* compile with -fno-strict-aliasing, like the Linux 
kernel:
https://lkml.org/lkml/2003/2/26/158
http://www.mail-archive.com/[email protected]/msg01647.html

If you look at miniz's CMakeLists.txt, it uses -fno-strict-aliasing.

Original comment by [email protected] on 2 Dec 2013 at 3:06

GoogleCodeExporter avatar May 21 '15 03:05 GoogleCodeExporter

This should be fixed now

uroni avatar Mar 11 '17 18:03 uroni

There are still errors when compiling with -fstrict-aliasing on 35c2d67bcf7a649d:

miniz.c: In function ‘tdefl_find_match’:
miniz.c:1348:5: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
     mz_uint16 c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]), s01 = TDEFL_READ_UNALIGNED_WORD(s);
     ^~~~~~~~~
miniz.c:1365:13: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
             TDEFL_PROBE;
             ^~~~~~~~~~~
miniz.c:1366:13: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
             TDEFL_PROBE;
             ^~~~~~~~~~~
miniz.c:1367:13: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
             TDEFL_PROBE;
             ^~~~~~~~~~~
miniz.c:1391:13: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
             c01 = TDEFL_READ_UNALIGNED_WORD(&d->m_dict[pos + match_len - 1]);
             ^~~
miniz.c: In function ‘mz_zip_validate_file’:
miniz.c:4935:9: warning: dereferencing type-punned pointer will break strict-aliasing rules [-Wstrict-aliasing]
         has_id = (MZ_READ_LE32(descriptor_buf) == MZ_ZIP_DATA_DESCRIPTOR_ID);
         ^~~~~~

janisozaur avatar Mar 24 '17 08:03 janisozaur

This can be alleviated by disabling the broken MINIZ_USE_UNALIGNED_LOADS_AND_STORES option in the config until a proper fix is in place.

janisozaur avatar Mar 24 '17 19:03 janisozaur

The issue still does not solve. So what should I do? Disable the warning in code or I must compile with -fno-strict-aliasing?

huyuguang avatar May 02 '17 09:05 huyuguang

Ok, sorry. Either set MINIZ_USE_UNALIGNED_LOADS_AND_STORES to zero (less performance on x86/x64), set -fno-strict-aliasing or ignore the warning. The warning should be a false positive.

uroni avatar May 04 '17 23:05 uroni

Thanks for reply. So it safe to disable the warning.

huyuguang avatar May 05 '17 08:05 huyuguang