Skip to content

Commit

Permalink
Fix memory corruption in the game browser when calling create while i…
Browse files Browse the repository at this point in the history
…terating file entries.

Looks like some memory reallocation issue because the cache receives new entries and the vector moves around.
  • Loading branch information
Ghabry committed Feb 13, 2025
1 parent 6260f41 commit 09462ca
Showing 1 changed file with 14 additions and 3 deletions.
17 changes: 14 additions & 3 deletions src/window_gamelist.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -57,15 +68,15 @@ 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 });
#endif
}
} 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 });
Expand Down

0 comments on commit 09462ca

Please sign in to comment.