Fix potential use of old names in group and engine name sorters

This could result in violation of strict weak ordering
pull/138/head
Jonathan G Rennison 4 years ago
parent fa90b56780
commit 4338541be8

@ -126,6 +126,9 @@ static bool EngineIntroDateSorter(const EngineID &a, const EngineID &b)
return _engine_sort_direction ? r > 0 : r < 0;
}
/* cached values for EngineNameSorter to spare many GetString() calls */
static EngineID _last_engine[2] = { INVALID_ENGINE, INVALID_ENGINE };
/**
* Determines order of engines by name
* @param a first engine to compare
@ -134,17 +137,16 @@ static bool EngineIntroDateSorter(const EngineID &a, const EngineID &b)
*/
static bool EngineNameSorter(const EngineID &a, const EngineID &b)
{
static EngineID last_engine[2] = { INVALID_ENGINE, INVALID_ENGINE };
static char last_name[2][64] = { "\0", "\0" };
static char last_name[2][64] = { "", "" };
if (a != last_engine[0]) {
last_engine[0] = a;
if (a != _last_engine[0]) {
_last_engine[0] = a;
SetDParam(0, a);
GetString(last_name[0], STR_ENGINE_NAME, lastof(last_name[0]));
}
if (b != last_engine[1]) {
last_engine[1] = b;
if (b != _last_engine[1]) {
_last_engine[1] = b;
SetDParam(0, b);
GetString(last_name[1], STR_ENGINE_NAME, lastof(last_name[1]));
}
@ -1302,6 +1304,9 @@ struct BuildVehicleWindow : Window {
this->SelectEngine(sel_id);
/* invalidate cached values for name sorter - engine names could change */
_last_engine[0] = _last_engine[1] = INVALID_ENGINE;
/* make engines first, and then wagons, sorted by selected sort_criteria */
_engine_sort_direction = false;
EngList_Sort(&this->eng_list, TrainEnginesThenWagonsSorter);

@ -556,6 +556,9 @@ static const int LEVEL_WIDTH = 10; ///< Indenting width of a sub-group in pixels
typedef GUIList<const Group*> GUIGroupList;
/* cached values for GroupNameSorter to spare many GetString() calls */
static const Group *_last_group[2] = { nullptr, nullptr };
/** Company livery colour scheme window. */
struct SelectCompanyLiveryWindow : public Window {
private:
@ -621,17 +624,16 @@ private:
static bool GroupNameSorter(const Group * const &a, const Group * const &b)
{
static const Group *last_group[2] = { nullptr, nullptr };
static char last_name[2][64] = { "", "" };
if (a != last_group[0]) {
last_group[0] = a;
if (a != _last_group[0]) {
_last_group[0] = a;
SetDParam(0, a->index);
GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0]));
}
if (b != last_group[1]) {
last_group[1] = b;
if (b != _last_group[1]) {
_last_group[1] = b;
SetDParam(0, b->index);
GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1]));
}
@ -669,6 +671,10 @@ private:
}
list.ForceResort();
/* invalidate cached values for name sorter - group names could change */
_last_group[0] = _last_group[1] = nullptr;
list.Sort(&GroupNameSorter);
AddChildren(&list, INVALID_GROUP, 0);

@ -106,20 +106,22 @@ static const NWidgetPart _nested_group_widgets[] = {
EndContainer(),
};
/* cached values for GroupNameSorter to spare many GetString() calls */
static const Group *_last_group[2] = { nullptr, nullptr };
/** Sort the groups by their name */
bool GroupNameSorter(const Group * const &a, const Group * const &b)
{
static const Group *last_group[2] = { nullptr, nullptr };
static char last_name[2][64] = { "", "" };
if (a != last_group[0]) {
last_group[0] = a;
if (a != _last_group[0]) {
_last_group[0] = a;
SetDParam(0, a->index);
GetString(last_name[0], STR_GROUP_NAME, lastof(last_name[0]));
}
if (b != last_group[1]) {
last_group[1] = b;
if (b != _last_group[1]) {
_last_group[1] = b;
SetDParam(0, b->index);
GetString(last_name[1], STR_GROUP_NAME, lastof(last_name[1]));
}
@ -207,6 +209,10 @@ private:
this->SetWidgetDisabledState(WID_GL_COLLAPSE_ALL_GROUPS, !enable_collapse_all);
list.ForceResort();
/* invalidate cached values for name sorter - group names could change */
_last_group[0] = _last_group[1] = nullptr;
list.Sort(&GroupNameSorter);
AddChildren(&list, INVALID_GROUP, 0);

Loading…
Cancel
Save