From b5b1957f58b3af54df596d7c2f0379004e2a777e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Thu, 12 Nov 2015 13:32:15 +0100 Subject: [PATCH] Fix some left-shifts An explicit `1` is considered signed by compiler, if `int` is 32 bit long, a left shift `1 << 31` is undefined. Since the type of result is the type of left shift operand, make sure we shift unsigned int so that the behaviour is defined correctly. Also one fix in peep code, where it would use too big value for `item_extra_flags`. --- src/peep/peep.c | 28 ++++++++++++++-------------- src/util/util.c | 4 ++-- src/windows/scenery.c | 6 +++--- 3 files changed, 19 insertions(+), 19 deletions(-) diff --git a/src/peep/peep.c b/src/peep/peep.c index d1d06e3ec7..e714c8de93 100644 --- a/src/peep/peep.c +++ b/src/peep/peep.c @@ -4147,7 +4147,7 @@ static void peep_update_using_bin(rct_peep* peep){ uint32 empty_containers = peep_empty_container_standard_flag(peep); for (uint8 cur_container = 0; cur_container < 32; cur_container++){ - if (!(empty_containers & (1 << cur_container))) continue; + if (!(empty_containers & (1u << cur_container))) continue; if (rubbish_in_bin != 0){ // OpenRCT2 modification: This previously used @@ -4177,7 +4177,7 @@ static void peep_update_using_bin(rct_peep* peep){ empty_containers = peep_empty_container_extra_flag(peep); for (uint8 cur_container = 0; cur_container < 32; cur_container++){ - if (!(empty_containers & (1 << cur_container))) continue; + if (!(empty_containers & (1u << cur_container))) continue; if (rubbish_in_bin != 0){ // OpenRCT2 modification: This previously used @@ -5741,9 +5741,9 @@ int peep_is_mechanic(rct_peep *peep) bool peep_has_item(rct_peep *peep, int peepItem) { if (peepItem < 32) { - return peep->item_standard_flags & (1 << peepItem); + return peep->item_standard_flags & (1u << peepItem); } else { - return peep->item_extra_flags & (1 << peepItem); + return peep->item_extra_flags & (1u << (peepItem - 32)); } } @@ -8608,7 +8608,7 @@ static void peep_pick_ride_to_go_on(rct_peep *peep) int i; FOR_ALL_RIDES(i, ride) { if (!peep_has_ridden(peep, i)) { - RCT2_ADDRESS(0x00F1AD98, uint32)[i >> 5] |= (1 << (i & 0x1F)); + RCT2_ADDRESS(0x00F1AD98, uint32)[i >> 5] |= (1u << (i & 0x1F)); } } } else { @@ -8623,7 +8623,7 @@ static void peep_pick_ride_to_go_on(rct_peep *peep) if (map_element_get_type(mapElement) != MAP_ELEMENT_TYPE_TRACK) continue; int rideIndex = mapElement->properties.track.ride_index; - RCT2_ADDRESS(0x00F1AD98, uint32)[rideIndex >> 5] |= (1 << (rideIndex & 0x1F)); + RCT2_ADDRESS(0x00F1AD98, uint32)[rideIndex >> 5] |= (1u << (rideIndex & 0x1F)); } while (!map_element_is_last_for_tile(mapElement++)); } } @@ -8636,7 +8636,7 @@ static void peep_pick_ride_to_go_on(rct_peep *peep) if (ride->excitement == (ride_rating)0xFFFF) continue; if (ride->highest_drop_height <= 66 && ride->excitement < RIDE_RATING(8,00)) continue; - RCT2_ADDRESS(0x00F1AD98, uint32)[i >> 5] |= (1 << (i & 0x1F)); + RCT2_ADDRESS(0x00F1AD98, uint32)[i >> 5] |= (1u << (i & 0x1F)); } } @@ -8645,7 +8645,7 @@ static void peep_pick_ride_to_go_on(rct_peep *peep) uint8 *nextPotentialRide = potentialRides; int numPotentialRides = 0; for (int i = 0; i < MAX_RIDES; i++) { - if (!(RCT2_ADDRESS(0x00F1AD98, uint32)[i >> 5] & (1 << (i & 0x1F)))) + if (!(RCT2_ADDRESS(0x00F1AD98, uint32)[i >> 5] & (1u << (i & 0x1F)))) continue; rct_ride *ride = GET_RIDE(i); @@ -8724,7 +8724,7 @@ static void peep_head_for_nearest_ride_type(rct_peep *peep, int rideType) int i; FOR_ALL_RIDES(i, ride) { if (ride->type == rideType) { - RCT2_ADDRESS(0x00F1AD98, uint32)[i >> 5] |= (1 << (i & 0x1F)); + RCT2_ADDRESS(0x00F1AD98, uint32)[i >> 5] |= (1u << (i & 0x1F)); } } } else { @@ -8741,7 +8741,7 @@ static void peep_head_for_nearest_ride_type(rct_peep *peep, int rideType) int rideIndex = mapElement->properties.track.ride_index; ride = GET_RIDE(rideIndex); if (ride->type == rideType) { - RCT2_ADDRESS(0x00F1AD98, uint32)[rideIndex >> 5] |= (1 << (rideIndex & 0x1F)); + RCT2_ADDRESS(0x00F1AD98, uint32)[rideIndex >> 5] |= (1u << (rideIndex & 0x1F)); } } while (!map_element_is_last_for_tile(mapElement++)); } @@ -8754,7 +8754,7 @@ static void peep_head_for_nearest_ride_type(rct_peep *peep, int rideType) uint8 *nextPotentialRide = potentialRides; int numPotentialRides = 0; for (int i = 0; i < MAX_RIDES; i++) { - if (!(RCT2_ADDRESS(0x00F1AD98, uint32)[i >> 5] & (1 << (i & 0x1F)))) + if (!(RCT2_ADDRESS(0x00F1AD98, uint32)[i >> 5] & (1u << (i & 0x1F)))) continue; rct_ride *ride = GET_RIDE(i); @@ -8836,7 +8836,7 @@ static void peep_head_for_nearest_ride_with_flags(rct_peep *peep, int rideTypeFl int i; FOR_ALL_RIDES(i, ride) { if (RCT2_ADDRESS(RCT2_ADDRESS_RIDE_FLAGS, uint32)[ride->type * 2] & rideTypeFlags) { - RCT2_ADDRESS(0x00F1AD98, uint32)[i >> 5] |= (1 << (i & 0x1F)); + RCT2_ADDRESS(0x00F1AD98, uint32)[i >> 5] |= (1u << (i & 0x1F)); } } } else { @@ -8853,7 +8853,7 @@ static void peep_head_for_nearest_ride_with_flags(rct_peep *peep, int rideTypeFl int rideIndex = mapElement->properties.track.ride_index; ride = GET_RIDE(rideIndex); if (RCT2_ADDRESS(RCT2_ADDRESS_RIDE_FLAGS, uint32)[ride->type * 2] & rideTypeFlags) { - RCT2_ADDRESS(0x00F1AD98, uint32)[rideIndex >> 5] |= (1 << (rideIndex & 0x1F)); + RCT2_ADDRESS(0x00F1AD98, uint32)[rideIndex >> 5] |= (1u << (rideIndex & 0x1F)); } } while (!map_element_is_last_for_tile(mapElement++)); } @@ -8866,7 +8866,7 @@ static void peep_head_for_nearest_ride_with_flags(rct_peep *peep, int rideTypeFl uint8 *nextPotentialRide = potentialRides; int numPotentialRides = 0; for (int i = 0; i < MAX_RIDES; i++) { - if (!(RCT2_ADDRESS(0x00F1AD98, uint32)[i >> 5] & (1 << (i & 0x1F)))) + if (!(RCT2_ADDRESS(0x00F1AD98, uint32)[i >> 5] & (1u << (i & 0x1F)))) continue; rct_ride *ride = GET_RIDE(i); diff --git a/src/util/util.c b/src/util/util.c index a1c93d7ebe..bfccdfa706 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -146,7 +146,7 @@ int bitscanforward(int source) return success != 0 ? i : -1; #else for (i = 0; i < 32; i++) - if (source & (1 << i)) + if (source & (1u << i)) return i; return -1; @@ -157,7 +157,7 @@ int bitcount(int source) { int result = 0; for (int i = 0; i < 32; i++) { - if (source & (1 << i)) { + if (source & (1u << i)) { result++; } } diff --git a/src/windows/scenery.c b/src/windows/scenery.c index f9f99c7c73..fd42a10f44 100644 --- a/src/windows/scenery.c +++ b/src/windows/scenery.c @@ -188,7 +188,7 @@ void window_scenery_update_scroll(rct_window *w); * The same code repeated five times for every scenery entry type */ void init_scenery_entry(rct_scenery_entry *sceneryEntry, int index, uint8 sceneryTabId) { - if (RCT2_ADDRESS(0x01357BD0, sint32)[index >> 5] & (1 << (index & 0x1F))) { + if (RCT2_ADDRESS(0x01357BD0, sint32)[index >> 5] & (1u << (index & 0x1F))) { if (sceneryTabId != 0xFF) { for (int i = 0; i < SCENERY_ENTRIES_BY_TAB; i++) { if (window_scenery_tab_entries[sceneryTabId][i] == -1) @@ -246,7 +246,7 @@ void init_scenery() for (int i = 0; i < scenerySetEntry->entry_count; i++) { uint16 sceneryEntryId = scenerySetEntry->scenery_entries[i]; uint32 ecx = RCT2_ADDRESS(0x01357BD0, uint32)[sceneryEntryId >> 5]; - uint32 edx = 1 << (sceneryEntryId & 0x1F); + uint32 edx = 1u << (sceneryEntryId & 0x1F); if (ecx & edx) { window_scenery_tab_entries[scenerySetIndex][sceneryTabEntryCount] = sceneryEntryId; window_scenery_tab_entries[scenerySetIndex][++sceneryTabEntryCount] = -1; @@ -1185,4 +1185,4 @@ void window_scenery_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, int scrol } sceneryTabItemIndex++; } -} \ No newline at end of file +}