diff --git a/src/ai/ai_gui.cpp b/src/ai/ai_gui.cpp index 3cfee3f6a5..bf03a47600 100644 --- a/src/ai/ai_gui.cpp +++ b/src/ai/ai_gui.cpp @@ -318,7 +318,7 @@ struct AISettingsWindow : public Window { SetDParam(idx++, current_value == 0 ? STR_CONFIG_SETTING_OFF : STR_CONFIG_SETTING_ON); } else { DrawArrowButtons(buttons_left, y + 2, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, editable && current_value > (*it).min_value, editable && current_value < (*it).max_value); - if (it->labels != NULL && it->labels->Find(current_value) != it->labels->End()) { + if (it->labels != NULL && it->labels->Contains(current_value)) { SetDParam(idx++, STR_JUST_RAW_STRING); SetDParamStr(idx++, it->labels->Find(current_value)->second); } else { diff --git a/src/ai/ai_info.cpp b/src/ai/ai_info.cpp index ff5dca946b..54725cd487 100644 --- a/src/ai/ai_info.cpp +++ b/src/ai/ai_info.cpp @@ -307,7 +307,8 @@ SQInteger AIInfo::AddLabels(HSQUIRRELVM vm) int key = atoi(key_string + 1); const char *label = SQ2OTTD(sq_label); - if (config->labels->Find(key) == config->labels->End()) config->labels->Insert(key, strdup(label)); + /* !Contains() prevents strdup from leaking. */ + if (!config->labels->Contains(key)) config->labels->Insert(key, strdup(label)); sq_pop(vm, 2); } diff --git a/src/core/smallmap_type.hpp b/src/core/smallmap_type.hpp index 8689447c14..7c01866894 100644 --- a/src/core/smallmap_type.hpp +++ b/src/core/smallmap_type.hpp @@ -54,6 +54,16 @@ struct SmallMap : SmallVector, S> { return this->End(); } + /** + * Tests whether a key is assigned in this map. + * @param key key to test + * @return true iff the item is present + */ + FORCEINLINE bool Contains(const T &key) + { + return this->Find(key) != this->End(); + } + /** * Removes given pair from this map * @param pair pair to remove @@ -90,7 +100,7 @@ struct SmallMap : SmallVector, S> { */ FORCEINLINE bool Insert(const T &key, const U &data) { - if (this->Find(key) != this->End()) return false; + if (this->Contains(key)) return false; Pair *n = this->Append(); n->first = key; n->second = data; diff --git a/src/network/core/address.cpp b/src/network/core/address.cpp index 586ee863a9..57edf50cb5 100644 --- a/src/network/core/address.cpp +++ b/src/network/core/address.cpp @@ -212,7 +212,7 @@ SOCKET NetworkAddress::Resolve(int family, int socktype, int flags, SocketList * * ofcourse totally unneeded ;) */ if (sockets != NULL) { NetworkAddress address(runp->ai_addr, (int)runp->ai_addrlen); - if (sockets->Find(address) != sockets->End()) continue; + if (sockets->Contains(address)) continue; } sock = func(runp); if (sock == INVALID_SOCKET) continue; diff --git a/src/newgrf_gui.cpp b/src/newgrf_gui.cpp index 2ec921d085..0e7934b68b 100644 --- a/src/newgrf_gui.cpp +++ b/src/newgrf_gui.cpp @@ -258,7 +258,7 @@ struct NewGRFParametersWindow : public Window { DrawArrowButtons(buttons_left, y + 2, COLOUR_YELLOW, (this->clicked_button == i) ? 1 + (this->clicked_increase != rtl) : 0, current_value > par_info->min_value, current_value < par_info->max_value); SetDParam(2, STR_JUST_INT); SetDParam(3, current_value); - if (par_info->value_names.Find(current_value) != par_info->value_names.End()) { + if (par_info->value_names.Contains(current_value)) { const char *label = GetGRFStringFromGRFText(par_info->value_names.Find(current_value)->second); if (label != NULL) { SetDParam(2, STR_JUST_RAW_STRING);