From 075b98e209ed1d5f9893efcde45ecbd49e246565 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Thu, 12 Nov 2015 12:15:20 +0100 Subject: [PATCH 1/3] Minor cleanups Mostly just setting variables to better define game state, with one minor update of decompilation where an `or` instruction was used in place of `mov` (in `sub_6CBCE2`). --- src/config.c | 2 +- src/peep/staff.c | 1 + src/ride/ride.c | 9 +++++---- src/scenario.c | 2 +- src/windows/map.c | 2 +- src/windows/park.c | 1 + src/windows/ride.c | 3 +++ src/windows/ride_construction.c | 2 +- src/windows/title_editor.c | 4 +++- src/windows/track_place.c | 1 + 10 files changed, 18 insertions(+), 9 deletions(-) diff --git a/src/config.c b/src/config.c index 647f59e4f7..cd6057d65b 100644 --- a/src/config.c +++ b/src/config.c @@ -1493,7 +1493,7 @@ static void title_sequence_open(const char *path, const char *customName) token = &parts[0 * 128]; part1 = &parts[1 * 128]; part2 = &parts[2 * 128]; - title_command command; + title_command command = { 0 }; command.command = 0xFF; if (token[0] != 0) { diff --git a/src/peep/staff.c b/src/peep/staff.c index 3d44d0cb53..b187d632b3 100644 --- a/src/peep/staff.c +++ b/src/peep/staff.c @@ -389,6 +389,7 @@ uint16 hire_new_staff_member(uint8 staffType) RCT2_GLOBAL(RCT2_ADDRESS_GAME_COMMAND_ERROR_TITLE, uint16) = STR_CANT_HIRE_NEW_STAFF; int eax, ebx, ecx, edx, esi, edi, ebp; + ecx = edx = esi = edi = ebp = 0; eax = 0x8000; ebx = staffType << 8 | GAME_COMMAND_FLAG_APPLY; diff --git a/src/ride/ride.c b/src/ride/ride.c index bb7261ad5b..573251d0d1 100644 --- a/src/ride/ride.c +++ b/src/ride/ride.c @@ -850,6 +850,7 @@ int ride_create_ride(ride_list_item listItem) ebx = GAME_COMMAND_FLAG_APPLY; edi = 0; esi = 0; + ebp = 0; RCT2_GLOBAL(RCT2_ADDRESS_GAME_COMMAND_ERROR_TITLE, uint16) = 0x3DC; @@ -1194,7 +1195,7 @@ int sub_6C683D(int* x, int* y, int* z, int direction, int type, uint16 extra_par mapElement->properties.track.colour &= 0x0F; mapElement->properties.track.colour |= (extra_params & 0xFF) << 4; } - + if (flags & (1 << 3)) { track_element_set_cable_lift(mapElement); } @@ -5562,7 +5563,7 @@ void game_command_callback_ride_remove_track_piece(int eax, int ebx, int ecx, in z = gRideRemoveTrackPieceCallbackZ; direction = gRideRemoveTrackPieceCallbackDirection; type = gRideRemoveTrackPieceCallbackType; - + window_ride_construction_mouseup_demolish_next_piece(x, y, z, direction, type); } @@ -6412,9 +6413,9 @@ static void ride_update_vehicle_colours(int rideIndex) { rct_ride *ride; rct_vehicle *vehicle; - rct_vehicle_colour colours; + rct_vehicle_colour colours = { 0 }; uint16 spriteIndex; - uint8 coloursExtended; + uint8 coloursExtended = 0; ride = GET_RIDE(rideIndex); if (ride->type == RIDE_TYPE_SPACE_RINGS || ride_type_has_flag(ride->type, RIDE_TYPE_FLAG_16)) { diff --git a/src/scenario.c b/src/scenario.c index 4d94b7be70..6975421c80 100644 --- a/src/scenario.c +++ b/src/scenario.c @@ -497,7 +497,7 @@ void scenario_success_submit_name(const char *name) **/ void scenario_entrance_fee_too_high_check() { - uint16 x, y; + uint16 x = 0, y = 0; uint16 totalRideValue = RCT2_GLOBAL(RCT2_TOTAL_RIDE_VALUE, uint16); uint16 park_entrance_fee = RCT2_GLOBAL(RCT2_ADDRESS_PARK_ENTRANCE_FEE, uint16); int max_fee = totalRideValue + (totalRideValue / 2); diff --git a/src/windows/map.c b/src/windows/map.c index 163604d879..7c46d62645 100644 --- a/src/windows/map.c +++ b/src/windows/map.c @@ -1597,7 +1597,7 @@ static uint16 map_window_get_pixel_colour_ride(int x, int y) static void map_window_set_pixels(rct_window *w) { - uint16 colour, *destination; + uint16 colour = 0, *destination; int x, y, dx, dy; destination = (uint16*)((RCT2_GLOBAL(0x00F1AD6C, uint32) * 511) + RCT2_GLOBAL(RCT2_ADDRESS_MAP_IMAGE_DATA, uint32) + 255); diff --git a/src/windows/park.c b/src/windows/park.c index 6b32b71bed..91be02ae90 100644 --- a/src/windows/park.c +++ b/src/windows/park.c @@ -1053,6 +1053,7 @@ static void window_park_entrance_paint(rct_window *w, rct_drawpixelinfo *dpi) static void window_park_init_viewport(rct_window *w) { int i, x, y, z, r, xy, zr, viewportFlags; + x = y = z = r = xy = zr = 0; rct_viewport *viewport; rct_widget *viewportWidget; diff --git a/src/windows/ride.c b/src/windows/ride.c index b04e0f2cb7..b6f44dde0b 100644 --- a/src/windows/ride.c +++ b/src/windows/ride.c @@ -5341,6 +5341,9 @@ static void window_ride_graphs_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi top = measurement->lateral[x] + 52; bottom = measurement->lateral[x + 1] + 52; break; + default: + log_error("Wrong graph type %d", listType); + top = bottom = 0; } top = widget->bottom - widget->top - top - 13; diff --git a/src/windows/ride_construction.c b/src/windows/ride_construction.c index 02de05c631..b3dae4b8de 100644 --- a/src/windows/ride_construction.c +++ b/src/windows/ride_construction.c @@ -2273,7 +2273,7 @@ static void sub_6CBCE2( offsetX = -trackBlock->y; offsetY = trackBlock->x; bl = rol8(bl, 3); - bh |= bl; + bh = bl; bh = ror8(bh, 4); bl &= 0x88; bh &= 0x77; diff --git a/src/windows/title_editor.c b/src/windows/title_editor.c index eb82afe2a8..41c2974acd 100644 --- a/src/windows/title_editor.c +++ b/src/windows/title_editor.c @@ -943,7 +943,7 @@ void window_title_editor_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, int gfx_fill_rect(dpi, x, y, x + SCROLL_WIDTH + 100, y + ROW_HEIGHT - 1, ColourMapA[w->colours[1]].lighter | 0x1000000); } - rct_string_id commandName; + rct_string_id commandName = STR_NONE; switch (command->command) { case TITLE_SCRIPT_LOAD: commandName = 5415; @@ -984,6 +984,8 @@ void window_title_editor_scrollpaint(rct_window *w, rct_drawpixelinfo *dpi, int case TITLE_SCRIPT_END: commandName = 5426; break; + default: + log_warning("Unknown command %d", command->command); } if ((selected || hover) && !error) { diff --git a/src/windows/track_place.c b/src/windows/track_place.c index c879b4907a..85365c9dc9 100644 --- a/src/windows/track_place.c +++ b/src/windows/track_place.c @@ -352,6 +352,7 @@ static void window_track_place_attempt_placement(int x, int y, int z, int bl, mo int eax, ebx, ecx, edx, esi, edi, ebp; money32 result; + edx = esi = ebp = 0; eax = x; ebx = bl; ecx = y; 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 2/3] 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 +} From 6723f51087b78078fbb4d62d73de549743f64616 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Janiszewski?= Date: Thu, 12 Nov 2015 14:08:32 +0100 Subject: [PATCH 3/3] Keep track of address returned by malloc to free it correctly When doing g2 generation on Linux, src variable was not modified in the same way as on Windows, which caused free() on a pointer that was not malloc()ed. This fixes #2219. --- src/cmdline_sprite.c | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/cmdline_sprite.c b/src/cmdline_sprite.c index d6e8ac2c21..5982135a92 100644 --- a/src/cmdline_sprite.c +++ b/src/cmdline_sprite.c @@ -264,6 +264,7 @@ bool sprite_file_import(const char *path, rct_g1_element *outElement, uint8 **ou // A larger range is needed for proper dithering sint16 *src = malloc(height * width * 4 * 2); + sint16 *src_orig = src; for (uint32 x = 0; x < height * width * 4; x++){ src[x] = (sint16) pixels[x]; } @@ -375,7 +376,7 @@ bool sprite_file_import(const char *path, rct_g1_element *outElement, uint8 **ou } } free(pixels); - free(src); + free(src_orig); int bufferLength = (int)(dst - buffer); buffer = realloc(buffer, bufferLength);