draconisPW / PWMAngband

A free, multi-player roguelike dungeon exploration game based on Angband
35 stars 12 forks source link

gcc warning: comparison is always false (ARM architecture) #539

Closed s88100 closed 2 years ago

s88100 commented 2 years ago

terminal output 'x86_64' and 'arm' pwma-sdl-armhf.txt pwma-sdl-x86_64.txt

'x86_64' terminal:
warning: comparison is always false due to limited range of data type

common/net-unix.c: In function ‘DgramConnect’:
common/net-unix.c:1712:33: warning: comparison is always false due to limited range of data type [-Wtype-limits]
 1712 |     if (addr_in.sin_addr.s_addr == (unsigned long)-1)
      |                                 ^~
common/net-unix.c: In function ‘DgramSend’:
common/net-unix.c:1814:31: warning: comparison of integer expressions of different signedness: ‘in_addr_t’ {aka ‘unsigned int’} and ‘int’ [-Wsign-compare]
 1814 |  if (the_addr.sin_addr.s_addr == (int)-1)
      |                               ^~

check please

common/net-unix.c
DgramConnect() :L1690
  if (addr_in.sin_addr.s_addr == (unsigned int)-1)
DgramSend() :L1785
  if (the_addr.sin_addr.s_addr == (unsigned int)-1)
CreateClientSocket() :L553
  if (peer.sin_addr.s_addr == (unsigned int)-1)

'armhf' terminal:
client/c-cmd.c: In function 'cmd_master_aux_generate_item_aux':
client/c-cmd.c:1377:28: warning: comparison is always false due to limited range of data type [-Wtype-limits]
                 if (buf[2] == -1) return 1;
                            ^
client/c-cmd.c: In function 'cmd_master_aux_summon_type':
client/c-cmd.c:1714:28: warning: comparison is always false due to limited range of data type [-Wtype-limits]
                 if (buf[1] == -1) return 1;
                            ^
client/c-cmd.c:1723:28: warning: comparison is always false due to limited range of data type [-Wtype-limits]
                 if (buf[1] == -1) return 1;
                            ^
client/c-cmd.c: In function 'cmd_master_aux_summon':
client/c-cmd.c:1800:28: warning: comparison is always false due to limited range of data type [-Wtype-limits]
                 if (buf[3] == -1) return 1;
client/ui-birth.c: In function 'get_char_name':
client/ui-birth.c:1923:33: warning: comparison is always false due to limited range of data type [-Wtype-limits]
         else if (char_expiry[i] == -1)
                                 ^
client/ui-birth.c:1970:80: warning: comparison is always false due to limited range of data type [-Wtype-limits]
         if ((i == (size_t)char_num) || (char_expiry[i] > 0) || (char_expiry[i] == -1))
                                                                                ^
client/ui-input.c: In function 'inkey_ex':
client/ui-input.c:189:25: warning: comparison is always false due to limited range of data type [-Wtype-limits]
         if ((inkey_scan == SCAN_INSTANT) && (0 != Term_inkey(&kk, false, false)))
                         ^
common/z-color.c: In function 'color_char_to_attr':
common/z-color.c:145:11: warning: comparison is always false due to limited range of data type [-Wtype-limits]
     if (c < 0) return (c);
           ^
server/netserver.c: In function 'Receive_features':
server/netserver.c:3496:19: warning: comparison is always false due to limited range of data type [-Wtype-limits]
     if ((lighting < 0) || (lighting >= LIGHTING_MAX)) discard = true;
                   ^
server/netserver.c: In function 'Receive_clear':
server/netserver.c:5231:15: warning: comparison is always false due to limited range of data type [-Wtype-limits]
     if ((mode < ES_KEY) || (mode > ES_END_MACRO))
               ^
server/main.c: In function 'server_log':
server/main.c:139:16: warning: comparison is always false due to limited range of data type [-Wtype-limits]
         if (*s < 0) *s = seven_bit_translation[128 + *s];
                ^

https://developer.arm.com/documentation/den0013/d/Porting/Miscellaneous-C-porting-issues/unsigned-char-and-signed-char tested on Android, workaround for users of GCC -fsigned-char

src/android/pwmangclient/jni/src/Android.mk
LOCAL_CFLAGS := -fsigned-char

https://quick-adviser.com/what-does-unsigned-char-mean-in-c/#Is_char_signed_in_C https://stackoverflow.com/questions/757482/comparison-is-always-true-due-to-limited-range-of-data-type-warning-in-c The C and C++ standards allows the character type char to be signed or unsigned, depending on the platform and compiler. Most systems, including x86 GNU/Linux and Microsoft Windows, use signed char, but those based on PowerPC and ARM processors typically use unsigned char. This can lead to unexpected results when porting programs between platforms which have different defaults for the type of char.

it would be nice to have support on ARM processors Raspberry Pi etc. recommend explicitly using signed char signed char: -127...127 unsigned char: 0...255

s88100 commented 2 years ago

processor architecture check

src/Makefile
# ARM Processor
ifneq ($(filter arm%, $(shell uname -m)),)
    # $(info processor "$(shell uname -m)")
    CFLAGS += -fsigned-char
endif

Linux Deploy 'armhf' terminal: uname -m armv7l

this should work for 'arm' processors

draconisPW commented 2 years ago

Should be fixed by #541.