diff options
Diffstat (limited to 'src')
-rw-r--r-- | src/ai/ai_gui.cpp | 2 | ||||
-rw-r--r-- | src/ai/ai_info.cpp | 3 | ||||
-rw-r--r-- | src/core/smallmap_type.hpp | 12 | ||||
-rw-r--r-- | src/network/core/address.cpp | 2 | ||||
-rw-r--r-- | src/newgrf_gui.cpp | 2 |
5 files changed, 16 insertions, 5 deletions
diff --git a/src/ai/ai_gui.cpp b/src/ai/ai_gui.cpp index 3cfee3f6a..bf03a4760 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 ff5dca946..54725cd48 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 8689447c1..7c0186689 100644 --- a/src/core/smallmap_type.hpp +++ b/src/core/smallmap_type.hpp @@ -55,6 +55,16 @@ struct SmallMap : SmallVector<SmallPair<T, U>, S> { } /** + * 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 * @note it has to be pointer to pair in this map. It is overwritten by the last item. @@ -90,7 +100,7 @@ struct SmallMap : SmallVector<SmallPair<T, U>, 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 586ee863a..57edf50cb 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 2ec921d08..0e7934b68 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); |