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;