From f96f5703ff1798cb5e071ccadef43a8198261909 Mon Sep 17 00:00:00 2001 From: Misa Date: Fri, 9 Apr 2021 08:45:24 -0700 Subject: [PATCH] Fix level list segfault when upgrading old levelstats.vvv So some people reported the levels list crashing when they loaded it. But this wasn't reproducible every time. They didn't provide any debugging information, so I had to use my backup plan: doing a full audit of the code path taken for loading the levels list. And then I found this. It turns out this was because I used a LOAD_ARRAY_RENAME() macro on an std::vector. You can't do that because you need to use push_back() to resize a vector, so the macro will end up indexing into nothing, causing a segfault. However, this code path would only be taken if you have an old levelstats.vvv, from 2.2 and previous - which explains why it wasn't 100% reproducible. But now that I know you need an old levelstats.vvv, this bug happens 100% of the time. Anyways, to fix this, just ditch the macro and expand it manually, while replacing the indexing with a proper usage of push_back(). --- desktop_version/src/Game.cpp | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/desktop_version/src/Game.cpp b/desktop_version/src/Game.cpp index 000144fa..3c28290f 100644 --- a/desktop_version/src/Game.cpp +++ b/desktop_version/src/Game.cpp @@ -556,7 +556,16 @@ void Game::loadcustomlevelstats(void) pText = ""; } - LOAD_ARRAY_RENAME(customlevelscore, customlevelscores) + if (SDL_strcmp(pKey, "customlevelscore") == 0 && pText[0] != '\0') + { + char buffer[16]; + size_t start = 0; + + while (next_split_s(buffer, sizeof(buffer), &start, pText, ',')) + { + customlevelscores.push_back(help.Int(buffer)); + } + } if (SDL_strcmp(pKey, "customlevelstats") == 0 && pText[0] != '\0') {