comparison is always true due to limited range of data type

3 posts / 0 new
Last post
salass00
salass00's picture
Offline
Last seen: 6 months 1 week ago
Joined: 2011-02-03 11:27
comparison is always true due to limited range of data type

The code I'm compiling (not written by me) has a macro for checking a version number and then doing something if the version falls within the specified limits:

  1. #define VER(file_version, first_version, last_version, block)\
  2. if ((((Uint16)file_version) >= ((Uint16)first_version)) && (((Uint16)file_version) <= ((Uint16)last_version)))\
  3. {\
  4. block;\
  5. }

The file_version passed in is an Uint8 and in some cases where this macro is used there is no upper limit for it so last_version is specified as 0xff so that the "file_version <= last_version" comparison will always be true. This is not a bug and is the wanted behavior but unfortunately gcc with -Wall refuses to shut up about it (I also use -Werror because I don't want to miss any warnings that might be bugs).


ppc-amigaos-gcc -std=gnu99 --no-strict-aliasing -O2 -Wall -Werror -I. -I/SDK/local/common/include/SDL -c -o snd/music.o snd/music.c
cc1: warnings being treated as errors
snd/music.c: In function 'load_wavetable_entry':
snd/music.c:1525: warning: comparison is always true due to limited range of data type
snd/music.c:1526: warning: comparison is always true due to limited range of data type
snd/music.c:1527: warning: comparison is always true due to limited range of data type
snd/music.c:1528: warning: comparison is always true due to limited range of data type
snd/music.c:1529: warning: comparison is always true due to limited range of data type
--- snip ---

Obviously the author of the code has tried to get around this warning by casting both values to Uint16 before doing this comparison but it seems that gcc 4.2.4 is "smart" enough to see through this ruse. I've tried adding "-Wno-type-limits" to stop this nonesense but unfortunately it is only supported in gcc >= 4.3 while the SDK gcc is version 4.2.4.

So does anyone know of an easy way to get around this apart from removing -Wall or -Werror options? Changing every invocation of the VER() macro in the code is not an option...

salass00
salass00's picture
Offline
Last seen: 6 months 1 week ago
Joined: 2011-02-03 11:27
Well I got around the problem

Well I got around the problem by defining a static inline function for doing the comparison and then using it inside the VER() macro like so:

  1. static inline bool _check_ver_(Uint16 file_version, Uint16 first_version, Uint16 last_version)
  2. {
  3. return (file_version >= first_version) && (file_version <= last_version);
  4. }
  5.  
  6. #define VER(file_version, first_version, last_version, block)\
  7. if (_check_ver_(file_version, first_version, last_version))\
  8. {\
  9. block;\
  10. }

Now I just wonder at which version gcc will be "smart" enough to see through this workaround...

OldFart
OldFart's picture
Offline
Last seen: 3 months 1 week ago
Joined: 2010-11-30 14:09
@salass00 Clobbered this one

@salass00

Clobbered this one together:

  1. #include <proto/exec.h>
  2. #include <proto/dos.h>
  3.  
  4. #define VER(file_version, first_version, last_version) \
  5.   {uint16 file = file_version, first = first_version, last = last_version; \
  6. if ( \
  7.   (file >= first) && \
  8.   (file <= last) \
  9.   ) \
  10. {IDOS->Printf("INFO : Fileversion within limits\n");} \
  11.   else {IDOS->Printf("ERROR: Fileversion out of limits\n");}}\
  12.  
  13.  
  14. int main(int argc UNUSED, char *argv[] UNUSED)
  15. {
  16. int RC = RETURN_FAIL;
  17.  
  18. uint8 version[4] = {3, 1, 2, 4};
  19.  
  20. VER(version[0], version[1], 0xff); //version[2]);
  21.  
  22. VER(version[0], version[3], 0xff); //version[3]);
  23.  
  24.  
  25. return RC;
  26. }

and compiled with this:

  1. SWITCHES = -W \
  2. -Werror \
  3. -Wmissing-prototypes \
  4. -Wsign-compare \
  5. -Wundef \
  6. -Wshadow \
  7. -Wwrite-strings \
  8. -Wall
  9.  
  10.  
  11. ProjecT : main.c
  12. gcc main.c -o ProjecT $(SWITCHES)
  13. @echo "=== Done making ================"

No more compile errors.

Output:

  1. INFO : Fileversion within limits
  2. ERROR: Fileversion out of limits

OldFart

Log in or register to post comments