From fe8d1630418639e52e8fe94fee42e9dbe022c1f9 Mon Sep 17 00:00:00 2001 From: Misa Date: Mon, 15 Feb 2021 19:18:46 -0800 Subject: [PATCH] Fix reading uninitialized memory when creating Menu::levellist Valgrind reported this. The error here is that the buffer here is only guaranteed to be initialized up until (and including) the null-terminator, by SDL_snprintf(). Iterating over the entire allocated buffer is bad and I should feel bad as the girl who wrote this code; doing that reads uninitialized memory and passes it to SDL_tolower(). As a bonus, the iterator increment is now a preincrement instead of a postincrement. --- desktop_version/src/Game.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/desktop_version/src/Game.cpp b/desktop_version/src/Game.cpp index 07f44e96..6b2287dc 100644 --- a/desktop_version/src/Game.cpp +++ b/desktop_version/src/Game.cpp @@ -6338,7 +6338,7 @@ void Game::createmenu( enum Menu::MenuName t, bool samemenu/*= false*/ ) } char text[menutextbytes]; SDL_snprintf(text, sizeof(text), "%s%s", prefix, ed.ListOfMetaData[i].title.c_str()); - for (size_t ii = 0; ii < SDL_arraysize(text); ii++) + for (size_t ii = 0; text[ii] != '\0'; ++ii) { text[ii] = SDL_tolower(text[ii]); }