summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorJonathan G Rennison <j.g.rennison@gmail.com>2020-06-15 18:36:33 +0100
committerNiels Martin Hansen <nielsm@indvikleren.dk>2020-06-18 12:38:43 +0200
commitd830a34394a1ef9e9fa574914a01a9dd79c5c2f9 (patch)
tree9535b17d3aa40adff80078848c5df0beeb7c47a4 /src
parent084b073e57817199fc679179d8b0e4520b10e606 (diff)
downloadopenttd-d830a34394a1ef9e9fa574914a01a9dd79c5c2f9.tar.xz
Fix: Violation of strict weak ordering in engine name sorter
This could be caused by an engine being renamed, and the old name being cached from a previous sort. See: #7838
Diffstat (limited to 'src')
-rw-r--r--src/build_vehicle_gui.cpp17
1 files changed, 11 insertions, 6 deletions
diff --git a/src/build_vehicle_gui.cpp b/src/build_vehicle_gui.cpp
index c3252f7b9..a761e9b13 100644
--- a/src/build_vehicle_gui.cpp
+++ b/src/build_vehicle_gui.cpp
@@ -125,6 +125,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
@@ -133,17 +136,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]));
}
@@ -1292,6 +1294,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);