From 854f27498416b5f075eb17924241d5efca9fabe4 Mon Sep 17 00:00:00 2001 From: Marko Lindqvist Date: Sat, 21 Jan 2023 05:23:13 +0200 Subject: [PATCH 12/12] Retire unit_activity_text() As not re-entrant, it caused client crashes. - Convert all callers to use re-entrant unit_activity_astr() - Remove the function See osdn #46559 Signed-off-by: Marko Lindqvist --- client/gui-qt/dialogs.cpp | 13 +++++++++---- client/gui-sdl2/citydlg.c | 6 +++++- client/gui-sdl2/dialogs.c | 21 +++++++++++++++++---- client/gui-sdl2/mapview.c | 5 ++++- client/text.c | 7 +++++-- common/unit.c | 15 --------------- common/unit.h | 1 - 7 files changed, 40 insertions(+), 28 deletions(-) diff --git a/client/gui-qt/dialogs.cpp b/client/gui-qt/dialogs.cpp index d572304da6..a6f357d2d1 100644 --- a/client/gui-qt/dialogs.cpp +++ b/client/gui-qt/dialogs.cpp @@ -3806,11 +3806,16 @@ void units_select::paint(QPainter *painter, QPaintEvent *event) f_size = &point_size; } if (highligh_num != -1 && highligh_num < unit_list.count()) { + struct astring addition = ASTRING_INIT; + punit = unit_list.at(highligh_num); - /* TRANS: HP - hit points */ - str2 = QString(_("%1 HP:%2/%3")).arg(QString(unit_activity_text(punit)), - QString::number(punit->hp), - QString::number(unit_type_get(punit)->hp)); + unit_activity_astr(punit, &addition); + + // TRANS: HP - hit points + str2 = QString(_("%1 HP:%2/%3")).arg(QString(astr_str(&addition)), + QString::number(punit->hp), + QString::number(unit_type_get(punit)->hp)); + astr_free(&addition); } str = QString(PL_("%1 unit", "%1 units", unit_list_size(utile->units))) diff --git a/client/gui-sdl2/citydlg.c b/client/gui-sdl2/citydlg.c index 2106752d66..8f0135ad20 100644 --- a/client/gui-sdl2/citydlg.c +++ b/client/gui-sdl2/citydlg.c @@ -31,6 +31,7 @@ #endif /* SDL2_PLAIN_INCLUDE */ /* utility */ +#include "astring.h" #include "bitvector.h" #include "fcintl.h" #include "log.h" @@ -712,10 +713,12 @@ static void create_present_supported_units_widget_list(struct unit_list *pList) unit_list_iterate(pList, pUnit) { const char *vetname; + struct astring addition = ASTRING_INIT; pUType = unit_type_get(pUnit); vetname = utype_veteran_name_translation(pUType, pUnit->veteran); pHome_City = game_city_by_number(pUnit->homecity); + unit_activity_astr(pUnit, &addition); fc_snprintf(cBuf, sizeof(cBuf), "%s (%d,%d,%s)%s%s\n%s\n(%d/%d)\n%s", utype_name_translation(pUType), pUType->attack_strength, @@ -723,9 +726,10 @@ static void create_present_supported_units_widget_list(struct unit_list *pList) move_points_text(pUType->move_rate, FALSE), (vetname != NULL ? "\n" : ""), (vetname != NULL ? vetname : ""), - unit_activity_text(pUnit), + astr_str(&addition), pUnit->hp, pUType->hp, pHome_City ? pHome_City->name : Q_("?homecity:None")); + astr_free(&addition); if (pCityDlg->page == SUPPORTED_UNITS_PAGE) { int pCity_near_dist; diff --git a/client/gui-sdl2/dialogs.c b/client/gui-sdl2/dialogs.c index 336255c8fd..f3fc577b37 100644 --- a/client/gui-sdl2/dialogs.c +++ b/client/gui-sdl2/dialogs.c @@ -31,6 +31,7 @@ #endif /* SDL2_PLAIN_INCLUDE */ /* utility */ +#include "astring.h" #include "bitvector.h" #include "fcintl.h" #include "log.h" @@ -1127,6 +1128,9 @@ void unit_select_dialog_popup(struct tile *ptile) vetname = utype_veteran_name_translation(pUnitType, pUnit->veteran); if (unit_owner(pUnit) == client.conn.playing) { + struct astring addition = ASTRING_INIT; + + unit_activity_astr(pUnit, &addition); fc_snprintf(cBuf , sizeof(cBuf), _("Contact %s (%d / %d) %s(%d,%d,%s) %s"), (vetname != NULL ? vetname : ""), pUnit->hp, pUnitType->hp, @@ -1134,7 +1138,8 @@ void unit_select_dialog_popup(struct tile *ptile) pUnitType->attack_strength, pUnitType->defense_strength, move_points_text(pUnitType->move_rate, FALSE), - unit_activity_text(pUnit)); + astr_str(&addition)); + astr_free(&addition); } else { int att_chance, def_chance; @@ -1889,6 +1894,9 @@ void popup_advanced_terrain_dialog(struct tile *ptile, Uint16 pos_x, Uint16 pos_ vetname = utype_veteran_name_translation(pUnitType, pUnit->veteran); if (unit_owner(pUnit) == client.conn.playing) { + struct astring addition = ASTRING_INIT; + + unit_activity_astr(pUnit, &addition); fc_snprintf(cBuf, sizeof(cBuf), _("Activate %s (%d / %d) %s (%d,%d,%s) %s"), (vetname != NULL ? vetname : ""), @@ -1897,8 +1905,9 @@ void popup_advanced_terrain_dialog(struct tile *ptile, Uint16 pos_x, Uint16 pos_ pUnitType->attack_strength, pUnitType->defense_strength, move_points_text(pUnitType->move_rate, FALSE), - unit_activity_text(pUnit)); - + astr_str(&addition)); + astr_free(&addition); + create_active_iconlabel(pBuf, pWindow->dst, pstr, cBuf, adv_unit_select_callback); pBuf->data.unit = pUnit; @@ -2001,6 +2010,9 @@ void popup_advanced_terrain_dialog(struct tile *ptile, Uint16 pos_x, Uint16 pos_ vetname = utype_veteran_name_translation(pUnitType, pUnit->veteran); if ((pCity && city_owner(pCity) == client.conn.playing) || (unit_owner(pUnit) == client.conn.playing)) { + struct astring addition = ASTRING_INIT; + + unit_activity_astr(pUnit, &addition); fc_snprintf(cBuf, sizeof(cBuf), _("Activate %s (%d / %d) %s (%d,%d,%s) %s"), (vetname != NULL ? vetname : ""), @@ -2009,7 +2021,8 @@ void popup_advanced_terrain_dialog(struct tile *ptile, Uint16 pos_x, Uint16 pos_ pUnitType->attack_strength, pUnitType->defense_strength, move_points_text(pUnitType->move_rate, FALSE), - unit_activity_text(pUnit)); + astr_str(&addition)); + astr_free(&addition); create_active_iconlabel(pBuf, pWindow->dst, pstr, cBuf, adv_unit_select_callback); diff --git a/client/gui-sdl2/mapview.c b/client/gui-sdl2/mapview.c index a56f6887ce..e19d888b51 100644 --- a/client/gui-sdl2/mapview.c +++ b/client/gui-sdl2/mapview.c @@ -801,6 +801,7 @@ void redraw_unit_info_label(struct unit_list *punitlist) unit_list_iterate(pTile->units, aunit) { SDL_Surface *tmp_surf; + struct astring addition = ASTRING_INIT; if (aunit == pUnit) { continue; @@ -809,6 +810,7 @@ void redraw_unit_info_label(struct unit_list *punitlist) pUType = unit_type_get(aunit); vetname = utype_veteran_name_translation(pUType, aunit->veteran); pHome_City = game_city_by_number(aunit->homecity); + unit_activity_astr(aunit, &addition); fc_snprintf(buffer, sizeof(buffer), "%s (%d,%d,%s)%s%s\n%s\n(%d/%d)\n%s", utype_name_translation(pUType), pUType->attack_strength, @@ -816,9 +818,10 @@ void redraw_unit_info_label(struct unit_list *punitlist) move_points_text(pUType->move_rate, FALSE), (vetname != NULL ? "\n" : ""), (vetname != NULL ? vetname : ""), - unit_activity_text(aunit), + astr_str(&addition), aunit->hp, pUType->hp, pHome_City ? city_name_get(pHome_City) : Q_("?homecity:None")); + astr_free(&addition); buf_surf = create_surf(tileset_full_tile_width(tileset), tileset_full_tile_height(tileset), SDL_SWSURFACE); diff --git a/client/text.c b/client/text.c index 74b88ec843..e8f1a41d17 100644 --- a/client/text.c +++ b/client/text.c @@ -1107,8 +1107,11 @@ const char *get_unit_info_label_text2(struct unit_list *punits, int linebreaks) astr_add_line(&str, _("Turns to target: %d to %d"), min, max); } } else if (count == 1) { - astr_add_line(&str, "%s", - unit_activity_text(unit_list_get(punits, 0))); + struct astring addition = ASTRING_INIT; + + unit_activity_astr(unit_list_get(punits, 0), &addition); + astr_add_line(&str, "%s", astr_str(&addition)); + astr_free(&addition); } else if (count > 1) { astr_add_line(&str, PL_("%d unit selected", "%d units selected", diff --git a/common/unit.c b/common/unit.c index 4cb7a5685f..d0dfcf620b 100644 --- a/common/unit.c +++ b/common/unit.c @@ -1184,21 +1184,6 @@ bv_extras get_unit_tile_pillage_set(const struct tile *ptile) return tgt_ret; } -/************************************************************************** - Return text describing the unit's current activity as a static string. - - FIXME: Convert all callers of this function to unit_activity_astr() - because this function is not re-entrant. -**************************************************************************/ -const char *unit_activity_text(const struct unit *punit) { - static struct astring str = ASTRING_INIT; - - astr_clear(&str); - unit_activity_astr(punit, &str); - - return astr_str(&str); -} - /************************************************************************** Append text describing the unit's current activity to the given astring. **************************************************************************/ diff --git a/common/unit.h b/common/unit.h index 4f4cb43033..803e55c66e 100644 --- a/common/unit.h +++ b/common/unit.h @@ -335,7 +335,6 @@ bool kills_citizen_after_attack(const struct unit *punit); struct astring; /* Forward declaration. */ void unit_activity_astr(const struct unit *punit, struct astring *astr); void unit_upkeep_astr(const struct unit *punit, struct astring *astr); -const char *unit_activity_text(const struct unit *punit); int get_transporter_capacity(const struct unit *punit); -- 2.39.0