From b0092a1df386394e8e5d56bafdcce4008090de01 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 15 Oct 2022 12:11:09 +0300 Subject: [PATCH 36/36] Make city name allocation dynamic See osdn #45786 Signed-off-by: Marko Lindqvist --- client/packhand.c | 6 +++--- common/city.c | 28 ++++++++++++++++++++++++---- common/city.h | 3 ++- server/cityhand.c | 6 +++--- server/edithand.c | 2 +- 5 files changed, 33 insertions(+), 12 deletions(-) diff --git a/client/packhand.c b/client/packhand.c index c906de3eda..625e6c1bbd 100644 --- a/client/packhand.c +++ b/client/packhand.c @@ -730,7 +730,7 @@ void handle_city_info(const struct packet_city_info *packet) return; } else { name_changed = (0 != strncmp(packet->name, pcity->name, - sizeof(pcity->name))); + MAX_LEN_CITYNAME)); while (trade_route_list_size(pcity->routes) > packet->traderoute_count) { struct trade_route *proute = trade_route_list_get(pcity->routes, -1); @@ -1212,14 +1212,14 @@ void handle_city_short_info(const struct packet_city_short_info *packet) return; } else { name_changed = (0 != strncmp(packet->name, pcity->name, - sizeof(pcity->name))); + MAX_LEN_CITYNAME)); /* Check if city descriptions should be updated */ if (gui_options.draw_city_names && name_changed) { update_descriptions = TRUE; } - sz_strlcpy(pcity->name, packet->name); + city_name_set(pcity, packet->name); memset(pcity->feel, 0, sizeof(pcity->feel)); memset(pcity->specialists, 0, sizeof(pcity->specialists)); diff --git a/common/city.c b/common/city.c index d3f239ea33..f887c9f51c 100644 --- a/common/city.c +++ b/common/city.c @@ -1113,15 +1113,31 @@ void city_choose_build_default(struct city *pcity) } } -#ifndef city_name_get /**********************************************************************//** Return the name of the city. **************************************************************************/ const char *city_name_get(const struct city *pcity) { - return pcity->name; + return (pcity->name != NULL) ? pcity->name : "City missing a name"; +} + +/**********************************************************************//** + Set a new name for the city. +**************************************************************************/ +void city_name_set(struct city *pcity, const char *new_name) +{ + if (pcity->name != NULL) { + free(pcity->name); + } + + if (strlen(new_name) < MAX_LEN_CITYNAME) { + pcity->name = fc_strdup(new_name); + } else { + log_warn(_("City name \"%s\" too long"), new_name); + pcity->name = fc_malloc(MAX_LEN_CITYNAME); + sz_strlcpy(pcity->name, new_name); + } } -#endif /* city_name_get */ /**********************************************************************//** Add a (positive or negative) value to the city size. As citizens is an @@ -3317,7 +3333,9 @@ struct city *create_city_virtual(struct player *pplayer, struct city *pcity = fc_calloc(1, sizeof(*pcity)); fc_assert_ret_val(NULL != name, NULL); /* No unnamed cities! */ - sz_strlcpy(pcity->name, name); + + /* Do this early, so any logging later will have the city name */ + city_name_set(pcity, name); pcity->tile = ptile; fc_assert_ret_val(NULL != pplayer, NULL); /* No unowned cities! */ @@ -3432,6 +3450,8 @@ void destroy_city_virtual(struct city *pcity) } } + free(pcity->name); + memset(pcity, 0, sizeof(*pcity)); /* ensure no pointers remain */ free(pcity); } diff --git a/common/city.h b/common/city.h index ac69ff5c49..23c301bf47 100644 --- a/common/city.h +++ b/common/city.h @@ -308,7 +308,7 @@ struct cm_parameter; /* defined in ./common/aicore/cm.h */ #pragma pack(push, 1) #endif struct city { - char name[MAX_LEN_CITYNAME]; + char *name; struct tile *tile; /* May be NULL, should check! */ struct player *owner; /* Cannot be NULL. */ struct player *original; /* Cannot be NULL. */ @@ -545,6 +545,7 @@ void set_city_production(struct city *pcity); /* properties */ const char *city_name_get(const struct city *pcity); +void city_name_set(struct city *pcity, const char *new_name); #define city_owner(_pcity_) (_pcity_)->owner #define city_tile(_pcity_) (_pcity_)->tile diff --git a/server/cityhand.c b/server/cityhand.c index 6cd405a895..2cafff75f9 100644 --- a/server/cityhand.c +++ b/server/cityhand.c @@ -469,7 +469,7 @@ void handle_city_change(struct player *pplayer, int city_id, } /**********************************************************************//** - 'struct packet_city_rename' handler. + Handle city rename request packet. **************************************************************************/ void handle_city_rename(struct player *pplayer, int city_id, const char *name) @@ -477,7 +477,7 @@ void handle_city_rename(struct player *pplayer, int city_id, struct city *pcity = player_city_by_number(pplayer, city_id); char message[1024]; - if (!pcity) { + if (pcity == NULL) { return; } @@ -487,7 +487,7 @@ void handle_city_rename(struct player *pplayer, int city_id, return; } - sz_strlcpy(pcity->name, name); + city_name_set(pcity, name); city_refresh(pcity); send_city_info(NULL, pcity); } diff --git a/server/edithand.c b/server/edithand.c index 8924823e45..fb2a159d8f 100644 --- a/server/edithand.c +++ b/server/edithand.c @@ -759,7 +759,7 @@ void handle_edit_city(struct connection *pc, notify_conn(pc->self, ptile, E_BAD_COMMAND, ftc_editor, _("Cannot edit city name: %s"), buf); } else { - sz_strlcpy(pcity->name, packet->name); + city_name_set(pcity, packet->name); changed = TRUE; } } -- 2.35.1