Skip to content

Commit

Permalink
Fix a crash when a city is destroyed during TC
Browse files Browse the repository at this point in the history
The code used city info after it was freed. This minimal patch makes sure that
this is not the case.

This introduces a break in the WYSIWYG idea when disbanding cities: gold upkeep
of disbanded cities will not be paid with GOLD_UPKEEP_MIXED and
GOLD_UPKEEP_NATION.
  • Loading branch information
lmoureaux committed Mar 6, 2021
1 parent f09efe2 commit 5533fbe
Showing 1 changed file with 12 additions and 10 deletions.
22 changes: 12 additions & 10 deletions server/cityturn.c
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,7 @@ static bool player_balance_treasury_units(struct player *pplayer);
static bool disband_city(struct city *pcity);

static void define_orig_production_values(struct city *pcity);
static void update_city_activity(struct city *pcity);
static bool update_city_activity(struct city *pcity);
static void nullify_caravan_and_disband_plus(struct city *pcity);
static bool city_illness_check(const struct city * pcity);

Expand Down Expand Up @@ -604,11 +604,11 @@ void update_city_activities(struct player *pplayer)
r = fc_rand(i);
/* update unit upkeep */
city_units_upkeep(cities[r]);
update_city_activity(cities[r]);

/* used based on 'gold_upkeep_style', see below */
nation_unit_upkeep += city_total_unit_gold_upkeep(cities[r]);
nation_impr_upkeep += city_total_impr_gold_upkeep(cities[r]);
if (update_city_activity(cities[r])) {
/* used based on 'gold_upkeep_style', see below */
nation_unit_upkeep += city_total_unit_gold_upkeep(cities[r]);
nation_impr_upkeep += city_total_impr_gold_upkeep(cities[r]);
}

cities[r] = cities[--i];
}
Expand Down Expand Up @@ -3034,14 +3034,15 @@ static bool city_handle_disorder(struct city *pcity)

/**************************************************************************
Called every turn, at end of turn, for every city.
Returns true if the city is still alive.
**************************************************************************/
static void update_city_activity(struct city *pcity)
static bool update_city_activity(struct city *pcity)
{
struct player *pplayer = city_owner(pcity);
int saved_id;

if (!pcity) {
return;
return FALSE;
}

if (city_refresh(pcity)) {
Expand Down Expand Up @@ -3140,7 +3141,7 @@ static void update_city_activity(struct city *pcity)

if (! city_build_stuff(pplayer, pcity)) {
/* City disbanded... */
return;
return FALSE;
}


Expand All @@ -3160,7 +3161,7 @@ static void update_city_activity(struct city *pcity)
city_populate(pcity, pplayer);
if (NULL == player_city_by_number(pplayer, saved_id)) {
/* City destroyed by famine */
return;
return FALSE;
}


Expand All @@ -3182,6 +3183,7 @@ static void update_city_activity(struct city *pcity)
auto_arrange_workers(pcity);
}
sanity_check_city(pcity);
return TRUE;
}

/*****************************************************************************
Expand Down

0 comments on commit 5533fbe

Please sign in to comment.