knik0 / faad2

Freeware Advanced Audio (AAC) Decoder faad2 mirror
https://sourceforge.net/projects/faac/
Other
175 stars 78 forks source link

Build failure with -Werror=strict-aliasing: ic_predict.c:58:16: error: dereferencing type-punned pointer will break strict-aliasing rules #163

Closed kostadinsh closed 1 year ago

kostadinsh commented 1 year ago

faad2 fails to build with -Werror=strict-aliasing added to CFLAGS and CXXFLAGS

gcc version: (Gentoo 13.1.1_p20230527 p3) 13.1.1 20230527

[  6%] Building C object CMakeFiles/faad.dir/libfaad/ic_predict.c.o
/usr/bin/cc -DAPPLY_DRC -DHAVE_INTTYPES_H=1 -DHAVE_MEMCPY=1 -DHAVE_STRINGS_H=1 -DHAVE_STRING_H=1 -DHAVE_SYS_STAT_H=1 -DHAVE_SYS_TYPES_H=1 -DPACKAGE_VERSION=\"2.10.1\" -DSTDC_HEADERS=1 -I/home/kostadin/faad2/build/include -I/home/kostadin/faad2/libfaad -O2 -Werror=strict-aliasing -Wall -pedantic -ffloat-store -MD -MT CMakeFiles/faad.dir/libfaad/ic_predict.c.o -MF CMakeFiles/faad.dir/libfaad/ic_predict.c.o.d -o CMakeFiles/faad.dir/libfaad/ic_predict.c.o -c /home/kostadin/faad2/libfaad/ic_predict.c
/home/kostadin/faad2/libfaad/ic_predict.c: In function ‘flt_round’:
/home/kostadin/faad2/libfaad/ic_predict.c:58:16: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
   58 |         *pf = *(float32_t*)&tmp1 + *(float32_t*)&tmp2 - *(float32_t*)&tmp;
      |                ^~~~~~~~~~~~~~~~~
/home/kostadin/faad2/libfaad/ic_predict.c:58:37: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
   58 |         *pf = *(float32_t*)&tmp1 + *(float32_t*)&tmp2 - *(float32_t*)&tmp;
      |                                     ^~~~~~~~~~~~~~~~~
/home/kostadin/faad2/libfaad/ic_predict.c:58:58: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
   58 |         *pf = *(float32_t*)&tmp1 + *(float32_t*)&tmp2 - *(float32_t*)&tmp;
      |                                                          ^~~~~~~~~~~~~~~~
/home/kostadin/faad2/libfaad/ic_predict.c:60:16: error: dereferencing type-punned pointer will break strict-aliasing rules [-Werror=strict-aliasing]
   60 |         *pf = *(float32_t*)&tmp;
      |                ^~~~~~~~~~~~~~~~
/home/kostadin/faad2/libfaad/ic_predict.c: In function ‘inv_quant_pred’:
/home/kostadin/faad2/libfaad/ic_predict.c:80:12: warning: ‘x’ is used uninitialized [-Wuninitialized]
   80 |     return x;
      |            ^
/home/kostadin/faad2/libfaad/ic_predict.c:76:15: note: ‘x’ declared here
   76 |     float32_t x;
      |               ^
cc1: some warnings being treated as errors

Steps to reproduce:

  1. clone the repo
  2. cd into the faad2 folder
  3. mkdir build && cd build
  4. run CFLAGS="-O2 -Werror=strict-aliasing" CXXFLAGS="-O2 -Werror=strict-aliasing" cmake ..
  5. run make VERBOSE=1
  6. Build failure

Gentoo bug: https://bugs.gentoo.org/859844

thesamesam commented 1 year ago

FWIW, the getdword_n bits in https://github.com/knik0/faad2/commit/a6ed5189b2c62b5d6fabc95132b2fc96a2be8727 look like aliasing volations too. I suspect https://github.com/projg2/portable-endianness can help there.

eustas commented 1 year ago

In that change I've just moved getdword_n in source code, but agree, it looks horrible at least - both type-punning and fall-through... Going to rewrite it. And take care of flt_round soon.

thesamesam commented 1 year ago

Thank you! (Yes, sorry, I was lazy with the link. Oops.)

Much cleaner now and I think some of the big-endian paths weren't right before either (indices looked possibly a bit off), so a double win! Many thanks.