From 3c61c642a9ca69ce52cb209965a5707018841f57 Mon Sep 17 00:00:00 2001 From: Peter Nelson Date: Sat, 9 Sep 2023 16:38:12 +0100 Subject: [PATCH] Codechange: Don't allocate a text effect with INVALID_TE_ID. Previously, despite INVALID_TE_ID existing, it was not checked during allocation and there was no limit to the number of text effects. --- src/texteff.cpp | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/src/texteff.cpp b/src/texteff.cpp index 98a7e85794..01dca6087c 100644 --- a/src/texteff.cpp +++ b/src/texteff.cpp @@ -43,7 +43,11 @@ TextEffectID AddTextEffect(StringID msg, int center, int y, uint8_t duration, Te if (_game_mode == GM_MENU) return INVALID_TE_ID; auto it = std::find_if(std::begin(_text_effects), std::end(_text_effects), [](const TextEffect &te) { return te.string_id == INVALID_STRING_ID; }); - if (it == std::end(_text_effects)) it = _text_effects.emplace(std::end(_text_effects)); + if (it == std::end(_text_effects)) { + /* _text_effects.size() is the maximum ID + 1 that has been allocated. We should not allocate INVALID_TE_ID or beyond. */ + if (_text_effects.size() >= INVALID_TE_ID) return INVALID_TE_ID; + it = _text_effects.emplace(std::end(_text_effects)); + } TextEffect &te = *it;