From 09462ca315bb78803b5ca8ca317e704222eb0f75 Mon Sep 17 00:00:00 2001 From: Ghabry Date: Thu, 13 Feb 2025 12:17:21 +0100 Subject: [PATCH] Fix memory corruption in the game browser when calling create while iterating file entries. Looks like some memory reallocation issue because the cache receives new entries and the vector moves around. --- src/window_gamelist.cpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/src/window_gamelist.cpp b/src/window_gamelist.cpp index 6fd0bb17d5..8949240d2c 100644 --- a/src/window_gamelist.cpp +++ b/src/window_gamelist.cpp @@ -37,10 +37,21 @@ bool Window_GameList::Refresh(FilesystemView filesystem_base, bool show_dotdot) this->show_dotdot = show_dotdot; - auto files = base_fs.ListDirectory(); +#ifndef USE_CUSTOM_FILEBUF + // Calling "Create" while iterating over the directory list appears to corrupt + // the file entries probably because of a reallocation due to caching new entries. + // Create a copy of the entries instead to workaround this issue. + DirectoryTree::DirectoryListType files = *base_fs.ListDirectory(); +#else + DirectoryTree::DirectoryListType* files = base_fs.ListDirectory(); +#endif // Find valid game diectories +#ifndef USE_CUSTOM_FILEBUF + for (auto& dir : files) { +#else for (auto& dir : *files) { +#endif assert(!dir.second.name.empty() && "VFS BUG: Empty filename in the folder"); #ifdef EMSCRIPTEN @@ -57,7 +68,7 @@ bool Window_GameList::Refresh(FilesystemView filesystem_base, bool show_dotdot) // The type is only determined on platforms with fast file IO (Windows and UNIX systems) // A platform is considered "fast" when it does not require our custom IO buffer #ifndef USE_CUSTOM_FILEBUF - auto fs = base_fs.Create(dir.second.name); + FilesystemView fs = base_fs.Create(dir.second.name); game_entries.push_back({ dir.second.name, FileFinder::GetProjectType(fs) }); #else game_entries.push_back({ dir.second.name, FileFinder::ProjectType::Unknown }); @@ -65,7 +76,7 @@ bool Window_GameList::Refresh(FilesystemView filesystem_base, bool show_dotdot) } } else if (dir.second.type == DirectoryTree::FileType::Directory) { #ifndef USE_CUSTOM_FILEBUF - auto fs = base_fs.Create(dir.second.name); + FilesystemView fs = base_fs.Create(dir.second.name); game_entries.push_back({ dir.second.name, FileFinder::GetProjectType(fs) }); #else game_entries.push_back({ dir.second.name, FileFinder::ProjectType::Unknown });