phoboslab / qoa

The “Quite OK Audio Format” for fast, lossy audio compression
MIT License
769 stars 42 forks source link

Fixed additional compiler warnings #50

Closed kieselsteini closed 5 months ago

kieselsteini commented 5 months ago

I tend to write my C code with -Wall -Wextra enabled. Unfortunately the qoa.h header does a lot of signed / unsigned int comparisons in the for loops. This is a fix for this issue. There is also some code in the encoder to measure the relative error to the source material. Some of those variables are not used at all when QOA_RECORD_TOTAL_ERROR is not set. I guarded all variable definition and usage blocks.

phoboslab commented 5 months ago

Thanks! Technically the warnings about signed/unsigned are a bit pedantic, as the values extracted from the headers could never exceed the signed int range, but I'll take it :]

The #ifdefs are good!

Slightly related, I've seen warnings for some declarations in the encode loop being unassigned... which are also false positives. Don't remember if that was clang or gcc. I'll look into it.

Which compiler did you use?

kieselsteini commented 5 months ago

Thanks for taking my PR. At the moment I don't see any other warnings using -Wall -Wextrawith Apple clang version 15.0.0 (clang-1500.3.9.4).

Using gcc-14 (Homebrew GCC 14.1.0) 14.1.0 I see the following errors, you're mentioning. I'll try to fix them as well.

qoa.h: In function 'qoa_encode_frame':
qoa.h:478:36: warning: 'best_slice' may be used uninitialized [-Wmaybe-uninitialized]
  478 |                         best_slice <<= (QOA_SLICE_LEN - slice_len) * 3;
      |                         ~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
qoa.h:397:38: note: 'best_slice' was declared here
  397 |                         qoa_uint64_t best_slice;
      |                                      ^~~~~~~~~~
qoa.h:467:45: warning: 'best_scalefactor' may be used uninitialized [-Wmaybe-uninitialized]
  467 |                         prev_scalefactor[c] = best_scalefactor;
      |                         ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~
qoa.h:399:29: note: 'best_scalefactor' was declared here
  399 |                         int best_scalefactor;
      |                             ^~~~~~~~~~~~~~~~