From 02788b17d038e30e612dcbf0719ec45a8fc54a43 Mon Sep 17 00:00:00 2001 From: matbo87 Date: Sat, 16 Sep 2023 12:43:42 +0200 Subject: [PATCH 1/3] Make sure, filename is not a nullptr or empty to avoid invalid paths --- source/3dsfiles.cpp | 24 +++++++++++++++++++----- source/3dsimpl.cpp | 30 ++++++++++++++++++++++-------- source/3dsmain.cpp | 17 ++++++++++++++--- source/3dsmenu.cpp | 11 +++++++---- source/Snes9x/cheats2.cpp | 16 ++++++++++++++++ source/Snes9x/spc7110.cpp | 8 ++++++++ 6 files changed, 86 insertions(+), 20 deletions(-) diff --git a/source/3dsfiles.cpp b/source/3dsfiles.cpp index 453cae2..6415c1d 100644 --- a/source/3dsfiles.cpp +++ b/source/3dsfiles.cpp @@ -227,6 +227,10 @@ void file3dsGoToParentDirectory(void) // Checks if file exists. //---------------------------------------------------------------------- bool IsFileExists(const char * filename) { + if (filename == nullptr || filename[0] == '\0') { + return false; + } + if (FILE * file = fopen(filename, "r")) { fclose(file); return true; @@ -475,6 +479,11 @@ std::string file3dsGetThumbnailFilenameByBasename(const std::string& basename, c // get the associated filename of the current game (e.g. savestate, config, border, etc.) std::string file3dsGetAssociatedFilename(const char* filename, const char* ext, const char* targetDir, bool trimmed) { + if (filename == nullptr || filename[0] == '\0') { + return ""; + } + + std::string associatedFilename; std::string basename = trimmed ? file3dsGetTrimmedFileBasename(filename, false) : file3dsGetFileBasename(filename, false); if (strcmp(ext, ".png") == 0 || strcmp(ext, ".chx") == 0 || strcmp(ext, ".cht") == 0) { @@ -489,20 +498,25 @@ std::string file3dsGetAssociatedFilename(const char* filename, const char* ext, std::string extension = ext != nullptr ? std::string(ext) : ""; - if (targetDir == "thumbnails") { - return file3dsGetThumbnailFilenameByBasename(basename, ext); + if (targetDir == "thumbnails") { + associatedFilename = file3dsGetThumbnailFilenameByBasename(basename, ext); + return associatedFilename; } if (targetDir != nullptr) { - return std::string(settings3DS.RootDir) + "/" + targetDir + "/" + basename + extension; + associatedFilename = std::string(settings3DS.RootDir) + "/" + std::string(targetDir) + "/" + basename + extension; + + return associatedFilename; } // if targetDir is undefined, use current game directory std::string dir = std::string(filename); size_t lastSlashPos = dir.find_last_of('/'); if (lastSlashPos != std::string::npos) { - return dir.substr(0, lastSlashPos) + "/" + basename + extension; + associatedFilename = dir.substr(0, lastSlashPos) + "/" + basename + extension; + } else { + associatedFilename = basename + extension; } - return basename + extension; + return associatedFilename; } \ No newline at end of file diff --git a/source/3dsimpl.cpp b/source/3dsimpl.cpp index d3b167b..2e7a5d7 100644 --- a/source/3dsimpl.cpp +++ b/source/3dsimpl.cpp @@ -432,6 +432,10 @@ void impl3dsSetBorderImage() { } else { borderFilename = file3dsGetAssociatedFilename(Memory.ROMFilename, ".png", "borders", true); } + + if (borderFilename.empty()) { + return; + } float borderAlpha = (float)(settings3DS.GameBorderOpacity) / OPACITY_STEPS; @@ -468,7 +472,10 @@ bool impl3dsLoadROM(char *romFilePath) if(loaded) { std::string path = file3dsGetAssociatedFilename(romFilePath, ".srm", "saves"); - Memory.LoadSRAM (path.c_str()); + + if (!path.empty()) { + Memory.LoadSRAM (path.c_str()); + } // ensure controller is always set to player 1 when rom has loaded Settings.SwapJoypads = 0; @@ -693,6 +700,10 @@ bool impl3dsSaveStateAuto() bool impl3dsSaveState(const char* filename) { + if (filename == nullptr || filename[0] == '\0') { + return false; + } + return Snapshot(filename); } @@ -728,6 +739,10 @@ bool impl3dsLoadStateAuto() bool impl3dsLoadState(const char* filename) { + if (filename == nullptr || filename[0] == '\0') { + return false; + } + bool success = S9xLoadSnapshot(filename); if (success) { @@ -858,9 +873,10 @@ bool impl3dsTakeScreenshot(const char*& path, bool menuOpen) { while (i <= 99) { ext = "." + std::to_string(i) + ".png"; - snprintf(tmp, _MAX_PATH - 1, "%s", file3dsGetAssociatedFilename(Memory.ROMFilename, ext.c_str(), "screenshots").c_str()); + std::string filename = file3dsGetAssociatedFilename(Memory.ROMFilename, ext.c_str(), "screenshots"); + snprintf(tmp, _MAX_PATH - 1, "%s", filename.c_str()); - if (!IsFileExists(tmp)) { + if (!filename.empty() && !IsFileExists(tmp)) { path = tmp; break; } @@ -978,17 +994,15 @@ void S9xAutoSaveSRAM (void) //CPU.AccumulatedAutoSaveTimer = 0; CPU.SRAMModified = false; - if (settings3DS.SecondScreenContent == CONTENT_INFO) { - menu3dsSetSecondScreenContent("Saving SRAM to SD card...", DIALOGCOLOR_CYAN); - } - // Bug fix: Instead of stopping CSND, we generate silence // like we did prior to v0.61 // snd3DS.generateSilence = true; std::string path = file3dsGetAssociatedFilename(Memory.ROMFilename, ".srm", "saves"); - Memory.SaveSRAM (path.c_str()); + if (!path.empty()) { + Memory.SaveSRAM (path.c_str()); + } // Bug fix: Instead of starting CSND, we continue to mix // like we did prior to v0.61 diff --git a/source/3dsmain.cpp b/source/3dsmain.cpp index 446a2c1..be96da8 100644 --- a/source/3dsmain.cpp +++ b/source/3dsmain.cpp @@ -106,7 +106,11 @@ size_t cacheThumbnails(std::vector& romFileNames, unsigned short if (romFileNames[j].Type == FileEntryType::File) { std::string thumbnailFilename = file3dsGetAssociatedFilename(romFileNames[j].Filename.c_str(), ".png", "thumbnails", true); - file3dsAddFileBufferToMemory(romFileNames[j].Filename, thumbnailFilename); + + if (!thumbnailFilename.empty()) { + file3dsAddFileBufferToMemory(romFileNames[j].Filename, thumbnailFilename); + } + menu3dsSetCurrentPercent(++currentCount, totalCount); } @@ -225,7 +229,10 @@ void initThumbnailThread() { char lastSelectedGame[_MAX_PATH]; strncpy(lastSelectedGame, romFileNameLastSelected, _MAX_PATH); std::string thumbnailFilename = file3dsGetAssociatedFilename(lastSelectedGame, ".png", "thumbnails", true); - StoredFile file = file3dsAddFileBufferToMemory(lastSelectedGame, thumbnailFilename); + + if (!thumbnailFilename.empty()) { + file3dsAddFileBufferToMemory(lastSelectedGame, thumbnailFilename); + } } // values have been taken from thread-basic example of 3ds-examples @@ -377,7 +384,7 @@ int resetConfigOptionSelected(int val) { if (val > 1) { std::string gameConfigFile = file3dsGetAssociatedFilename(Memory.ROMFilename, ".cfg", "configs"); - if (std::remove(gameConfigFile.c_str()) != 0) { + if (!gameConfigFile.empty() && std::remove(gameConfigFile.c_str()) != 0) { cfgRemovalfailed += 2; } } @@ -1232,6 +1239,10 @@ bool settingsReadWriteFullListByGame(bool writeMode) BufferedFileWriter stream; std::string path = file3dsGetAssociatedFilename(Memory.ROMFilename, ".cfg", "configs"); + if (path.empty()) { + return false; + } + if (writeMode) { if (!stream.open(path.c_str(), "w")) return false; diff --git a/source/3dsmenu.cpp b/source/3dsmenu.cpp index 790ecc1..56b2ace 100644 --- a/source/3dsmenu.cpp +++ b/source/3dsmenu.cpp @@ -1319,12 +1319,15 @@ void menu3dsSetSecondScreenContent(const char *dialogMessage, int dialogBackgrou if (settings3DS.SecondScreenContent == CONTENT_IMAGE) { std::string coverFilename = file3dsGetAssociatedFilename(Memory.ROMFilename, ".png", "covers", true); - cover = file3dsAddFileBufferToMemory("gameCover", coverFilename); - // show fallback cover - if (cover.Buffer.empty() && settings3DS.RomFsLoaded) { - coverFilename = "romfs:/cover.png"; + if (!coverFilename.empty()) { cover = file3dsAddFileBufferToMemory("gameCover", coverFilename); + + // show fallback cover + if (cover.Buffer.empty() && settings3DS.RomFsLoaded) { + coverFilename = "romfs:/cover.png"; + cover = file3dsAddFileBufferToMemory("gameCover", coverFilename); + } } } diff --git a/source/Snes9x/cheats2.cpp b/source/Snes9x/cheats2.cpp index 294ac43..a3dcbdb 100644 --- a/source/Snes9x/cheats2.cpp +++ b/source/Snes9x/cheats2.cpp @@ -172,6 +172,10 @@ bool S9xCheatExists(uint32 addr) bool8 S9xLoadCheatFile (const char *filename) { + if (filename == nullptr || filename[0] == '\0') { + return false; + } + Cheat.num_cheats = 0; FILE *fs = fopen (filename, "rb"); @@ -205,6 +209,10 @@ bool8 S9xLoadCheatFile (const char *filename) bool8 S9xSaveCheatFile (const char *filename) { + if (filename == nullptr || filename[0] == '\0') { + return false; + } + if (Cheat.text_format) return false; @@ -269,6 +277,10 @@ void S9xStripNewLine(char *s) // bool8 S9xSaveCheatTextFile (const char *filename) { + if (filename == nullptr || filename[0] == '\0') { + return false; + } + if (!Cheat.text_format) return false; @@ -303,6 +315,10 @@ bool8 S9xSaveCheatTextFile (const char *filename) // bool8 S9xLoadCheatTextFile (const char *filename) { + if (filename == nullptr || filename[0] == '\0') { + return false; + } + FILE *fp = fopen (filename, "r"); if (fp == NULL) return false; diff --git a/source/Snes9x/spc7110.cpp b/source/Snes9x/spc7110.cpp index 5241357..0fb6657 100644 --- a/source/Snes9x/spc7110.cpp +++ b/source/Snes9x/spc7110.cpp @@ -2234,6 +2234,10 @@ bool8 S9xSaveSPC7110RTC (S7RTC *rtc_f9) FILE* fp; std::string path = file3dsGetAssociatedFilename(Memory.ROMFilename, ".rtc", NULL, false); + if (path.empty()) { + return (FALSE); + } + if((fp=fopen(path.c_str(), "wb"))==NULL) return (FALSE); int i=0; @@ -2265,6 +2269,10 @@ bool8 S9xLoadSPC7110RTC (S7RTC *rtc_f9) FILE* fp; std::string path = file3dsGetAssociatedFilename(Memory.ROMFilename, ".rtc", NULL, false); + if (path.empty()) { + return (FALSE); + } + if((fp=fopen(path.c_str(), "rb"))==NULL) return (FALSE); for (int i=0; i<16;i++) From 0e822f5eda8ee35415738f74dd4d5f203fce8022 Mon Sep 17 00:00:00 2001 From: matbo87 Date: Sat, 16 Sep 2023 12:59:36 +0200 Subject: [PATCH 2/3] This may not be necessary, but just in case --- source/3dsimpl.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/source/3dsimpl.cpp b/source/3dsimpl.cpp index 2e7a5d7..a0f2c0d 100644 --- a/source/3dsimpl.cpp +++ b/source/3dsimpl.cpp @@ -1001,7 +1001,9 @@ void S9xAutoSaveSRAM (void) std::string path = file3dsGetAssociatedFilename(Memory.ROMFilename, ".srm", "saves"); if (!path.empty()) { - Memory.SaveSRAM (path.c_str()); + char copiedPath[PATH_MAX]; + strcpy(copiedPath, path.c_str()); + Memory.SaveSRAM (copiedPath); } // Bug fix: Instead of starting CSND, we continue to mix From 200bedb4087111d07588cabcd9aef08279705a34 Mon Sep 17 00:00:00 2001 From: matbo87 Date: Sun, 17 Sep 2023 09:13:56 +0200 Subject: [PATCH 3/3] Revert "This may not be necessary, but just in case" This reverts commit 0e822f5eda8ee35415738f74dd4d5f203fce8022. --- source/3dsimpl.cpp | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/source/3dsimpl.cpp b/source/3dsimpl.cpp index a0f2c0d..2e7a5d7 100644 --- a/source/3dsimpl.cpp +++ b/source/3dsimpl.cpp @@ -1001,9 +1001,7 @@ void S9xAutoSaveSRAM (void) std::string path = file3dsGetAssociatedFilename(Memory.ROMFilename, ".srm", "saves"); if (!path.empty()) { - char copiedPath[PATH_MAX]; - strcpy(copiedPath, path.c_str()); - Memory.SaveSRAM (copiedPath); + Memory.SaveSRAM (path.c_str()); } // Bug fix: Instead of starting CSND, we continue to mix