From: Holger Schemel Date: Thu, 20 Oct 2022 15:35:49 +0000 (+0200) Subject: fixed undefined signed integer overflow for expression "1 << 31" X-Git-Tag: 4.3.3.0~49 X-Git-Url: https://git.artsoft.org/?a=commitdiff_plain;ds=inline;h=13fc9c40050cdd760fd457a9c83d0611f95fcd1e;hp=dfb50dc0bd7586c343a8cd16008644dceda92adf;p=rocksndiamonds.git fixed undefined signed integer overflow for expression "1 << 31" This issue was found by Cppcheck (static C/C++ code analysis tool) and by using Clang with option "-fsanitize=undefined". --- diff --git a/src/editor.c b/src/editor.c index 120a5891..833dce2e 100644 --- a/src/editor.c +++ b/src/editor.c @@ -2079,38 +2079,38 @@ static struct ValueTextInfo options_change_trigger_player[] = static struct ValueTextInfo options_change_trigger_page[] = { - { (1 << 0), "1" }, - { (1 << 1), "2" }, - { (1 << 2), "3" }, - { (1 << 3), "4" }, - { (1 << 4), "5" }, - { (1 << 5), "6" }, - { (1 << 6), "7" }, - { (1 << 7), "8" }, - { (1 << 8), "9" }, - { (1 << 9), "10" }, - { (1 << 10), "11" }, - { (1 << 11), "12" }, - { (1 << 12), "13" }, - { (1 << 13), "14" }, - { (1 << 14), "15" }, - { (1 << 15), "16" }, - { (1 << 16), "17" }, - { (1 << 17), "18" }, - { (1 << 18), "19" }, - { (1 << 19), "20" }, - { (1 << 20), "21" }, - { (1 << 21), "22" }, - { (1 << 22), "23" }, - { (1 << 23), "24" }, - { (1 << 24), "25" }, - { (1 << 25), "26" }, - { (1 << 26), "27" }, - { (1 << 27), "28" }, - { (1 << 28), "29" }, - { (1 << 29), "30" }, - { (1 << 30), "31" }, - { (1 << 31), "32" }, + { (1u << 0), "1" }, + { (1u << 1), "2" }, + { (1u << 2), "3" }, + { (1u << 3), "4" }, + { (1u << 4), "5" }, + { (1u << 5), "6" }, + { (1u << 6), "7" }, + { (1u << 7), "8" }, + { (1u << 8), "9" }, + { (1u << 9), "10" }, + { (1u << 10), "11" }, + { (1u << 11), "12" }, + { (1u << 12), "13" }, + { (1u << 13), "14" }, + { (1u << 14), "15" }, + { (1u << 15), "16" }, + { (1u << 16), "17" }, + { (1u << 17), "18" }, + { (1u << 18), "19" }, + { (1u << 19), "20" }, + { (1u << 20), "21" }, + { (1u << 21), "22" }, + { (1u << 22), "23" }, + { (1u << 23), "24" }, + { (1u << 24), "25" }, + { (1u << 25), "26" }, + { (1u << 26), "27" }, + { (1u << 27), "28" }, + { (1u << 28), "29" }, + { (1u << 29), "30" }, + { (1u << 30), "31" }, + { (1u << 31), "32" }, { CH_PAGE_ANY, "any" }, { -1, NULL } diff --git a/src/files.c b/src/files.c index 5e95fb80..5d8f6479 100644 --- a/src/files.c +++ b/src/files.c @@ -2848,7 +2848,7 @@ static int LoadLevel_CUS3(File *file, int chunk_size, struct LevelInfo *level) // bits 0 - 31 of "has_event[]" event_bits = getFile32BitBE(file); for (j = 0; j < MIN(NUM_CHANGE_EVENTS, 32); j++) - if (event_bits & (1 << j)) + if (event_bits & (1u << j)) ei->change->has_event[j] = TRUE; ei->change->target_element = getMappedElement(getFile16BitBE(file)); @@ -2984,7 +2984,7 @@ static int LoadLevel_CUS4(File *file, int chunk_size, struct LevelInfo *level) // bits 0 - 31 of "has_event[]" ... event_bits = getFile32BitBE(file); for (j = 0; j < MIN(NUM_CHANGE_EVENTS, 32); j++) - if (event_bits & (1 << j)) + if (event_bits & (1u << j)) change->has_event[j] = TRUE; change->target_element = getMappedElement(getFile16BitBE(file)); @@ -3025,7 +3025,7 @@ static int LoadLevel_CUS4(File *file, int chunk_size, struct LevelInfo *level) // ... bits 32 - 39 of "has_event[]" (not nice, but downward compatible) event_bits = getFile8Bit(file); for (j = 32; j < NUM_CHANGE_EVENTS; j++) - if (event_bits & (1 << (j - 32))) + if (event_bits & (1u << (j - 32))) change->has_event[j] = TRUE; } @@ -7327,7 +7327,7 @@ static void SaveLevel_CUS4(FILE *file, struct LevelInfo *level, int element) event_bits = 0; for (j = 0; j < MIN(NUM_CHANGE_EVENTS, 32); j++) if (change->has_event[j]) - event_bits |= (1 << j); + event_bits |= (1u << j); putFile32BitBE(file, event_bits); putFile16BitBE(file, change->target_element); @@ -7367,7 +7367,7 @@ static void SaveLevel_CUS4(FILE *file, struct LevelInfo *level, int element) event_bits = 0; for (j = 32; j < NUM_CHANGE_EVENTS; j++) if (change->has_event[j]) - event_bits |= (1 << (j - 32)); + event_bits |= (1u << (j - 32)); putFile8Bit(file, event_bits); } } diff --git a/src/main.h b/src/main.h index c5ff5f8c..fd70216d 100644 --- a/src/main.h +++ b/src/main.h @@ -200,7 +200,7 @@ #define EP_BITMASK_BASE_DEFAULT (1 << EP_CAN_MOVE_INTO_ACID) #define EP_BITMASK_DEFAULT 0 -#define PROPERTY_BIT(p) (1 << ((p) % 32)) +#define PROPERTY_BIT(p) (1u << ((p) % 32)) #define PROPERTY_VAR(e,p) (element_info[e].properties[(p) / 32]) #define HAS_PROPERTY(e,p) ((PROPERTY_VAR(e, p) & PROPERTY_BIT(p)) != 0) #define SET_PROPERTY(e,p,v) ((v) ? \ diff --git a/src/tape.h b/src/tape.h index d78c4b8f..8b375ede 100644 --- a/src/tape.h +++ b/src/tape.h @@ -64,39 +64,39 @@ #define VIDEO_CONTROL_YSIZE VIDEO_BUTTON_YSIZE // values for video tape control -#define VIDEO_STATE_PLAY_OFF (1 << 0) -#define VIDEO_STATE_PLAY_ON (1 << 1) -#define VIDEO_STATE_REC_OFF (1 << 2) -#define VIDEO_STATE_REC_ON (1 << 3) -#define VIDEO_STATE_PAUSE_OFF (1 << 4) -#define VIDEO_STATE_PAUSE_ON (1 << 5) -#define VIDEO_STATE_DATE_OFF (1 << 6) -#define VIDEO_STATE_DATE_ON (1 << 7) -#define VIDEO_STATE_TIME_OFF (1 << 8) -#define VIDEO_STATE_TIME_ON (1 << 9) -#define VIDEO_STATE_FRAME_OFF (1 << 10) -#define VIDEO_STATE_FRAME_ON (1 << 11) -#define VIDEO_STATE_FFWD_OFF (1 << 12) -#define VIDEO_STATE_FFWD_ON (1 << 13) -#define VIDEO_STATE_WARP_OFF (1 << 14) -#define VIDEO_STATE_WARP_ON (1 << 15) -#define VIDEO_STATE_WARP2_OFF (1 << 16) -#define VIDEO_STATE_WARP2_ON (1 << 17) -#define VIDEO_STATE_PBEND_OFF (1 << 18) -#define VIDEO_STATE_PBEND_ON (1 << 19) -#define VIDEO_STATE_1STEP_OFF (1 << 20) -#define VIDEO_STATE_1STEP_ON (1 << 21) - -#define VIDEO_PRESS_PLAY_ON (1 << 22) -#define VIDEO_PRESS_PLAY_OFF (1 << 23) -#define VIDEO_PRESS_REC_ON (1 << 24) -#define VIDEO_PRESS_REC_OFF (1 << 25) -#define VIDEO_PRESS_PAUSE_ON (1 << 26) -#define VIDEO_PRESS_PAUSE_OFF (1 << 27) -#define VIDEO_PRESS_STOP_ON (1 << 28) -#define VIDEO_PRESS_STOP_OFF (1 << 29) -#define VIDEO_PRESS_EJECT_ON (1 << 30) -#define VIDEO_PRESS_EJECT_OFF (1 << 31) +#define VIDEO_STATE_PLAY_OFF (1u << 0) +#define VIDEO_STATE_PLAY_ON (1u << 1) +#define VIDEO_STATE_REC_OFF (1u << 2) +#define VIDEO_STATE_REC_ON (1u << 3) +#define VIDEO_STATE_PAUSE_OFF (1u << 4) +#define VIDEO_STATE_PAUSE_ON (1u << 5) +#define VIDEO_STATE_DATE_OFF (1u << 6) +#define VIDEO_STATE_DATE_ON (1u << 7) +#define VIDEO_STATE_TIME_OFF (1u << 8) +#define VIDEO_STATE_TIME_ON (1u << 9) +#define VIDEO_STATE_FRAME_OFF (1u << 10) +#define VIDEO_STATE_FRAME_ON (1u << 11) +#define VIDEO_STATE_FFWD_OFF (1u << 12) +#define VIDEO_STATE_FFWD_ON (1u << 13) +#define VIDEO_STATE_WARP_OFF (1u << 14) +#define VIDEO_STATE_WARP_ON (1u << 15) +#define VIDEO_STATE_WARP2_OFF (1u << 16) +#define VIDEO_STATE_WARP2_ON (1u << 17) +#define VIDEO_STATE_PBEND_OFF (1u << 18) +#define VIDEO_STATE_PBEND_ON (1u << 19) +#define VIDEO_STATE_1STEP_OFF (1u << 20) +#define VIDEO_STATE_1STEP_ON (1u << 21) + +#define VIDEO_PRESS_PLAY_ON (1u << 22) +#define VIDEO_PRESS_PLAY_OFF (1u << 23) +#define VIDEO_PRESS_REC_ON (1u << 24) +#define VIDEO_PRESS_REC_OFF (1u << 25) +#define VIDEO_PRESS_PAUSE_ON (1u << 26) +#define VIDEO_PRESS_PAUSE_OFF (1u << 27) +#define VIDEO_PRESS_STOP_ON (1u << 28) +#define VIDEO_PRESS_STOP_OFF (1u << 29) +#define VIDEO_PRESS_EJECT_ON (1u << 30) +#define VIDEO_PRESS_EJECT_OFF (1u << 31) #define VIDEO_STATE_PLAY(x) ((x) ? VIDEO_STATE_PLAY_ON : VIDEO_STATE_PLAY_OFF) #define VIDEO_STATE_REC(x) ((x) ? VIDEO_STATE_REC_ON : VIDEO_STATE_REC_OFF)