From 87dc5ce43213ccf9a3a5813c72cbafcb0c4bed8b Mon Sep 17 00:00:00 2001 From: Kevin <68612569+diyelectromusic@users.noreply.github.com> Date: Sun, 14 May 2023 14:29:47 +0100 Subject: [PATCH] Issue470 fix - Performance file bounds checking and error handling (#494) * Fix for Issue #470 to introduce bounds checking on loading performance files and when saving a new performance. * Reduce number of performances to 256 and include some better error handling for when there are no performances free for saving. --- src/minidexed.cpp | 2 +- src/performanceconfig.cpp | 56 +++++++++++++++++++++++++++++---------- src/performanceconfig.h | 4 ++- 3 files changed, 46 insertions(+), 16 deletions(-) diff --git a/src/minidexed.cpp b/src/minidexed.cpp index 0bb7c14..fafeff8 100644 --- a/src/minidexed.cpp +++ b/src/minidexed.cpp @@ -1474,7 +1474,7 @@ bool CMiniDexed::DoSetNewPerformance (void) bool CMiniDexed::SavePerformanceNewFile () { - m_bSavePerformanceNewFile = m_PerformanceConfig.GetInternalFolderOk(); + m_bSavePerformanceNewFile = m_PerformanceConfig.GetInternalFolderOk() && m_PerformanceConfig.CheckFreePerformanceSlot(); return m_bSavePerformanceNewFile; } diff --git a/src/performanceconfig.cpp b/src/performanceconfig.cpp index 7249003..853ccf5 100644 --- a/src/performanceconfig.cpp +++ b/src/performanceconfig.cpp @@ -20,10 +20,13 @@ // You should have received a copy of the GNU General Public License // along with this program. If not, see . // +#include #include "performanceconfig.h" #include "mididevice.h" #include -#include +#include + +LOGMODULE ("Performance"); CPerformanceConfig::CPerformanceConfig (FATFS *pFileSystem) : m_Properties ("performance.ini", pFileSystem) @@ -747,8 +750,27 @@ bool CPerformanceConfig::GetInternalFolderOk() return nInternalFolderOk; } +bool CPerformanceConfig::CheckFreePerformanceSlot(void) +{ + if (nLastPerformance < NUM_PERFORMANCES) + { + // There is a free slot... + return true; + } + else + { + return false; + } +} + bool CPerformanceConfig::CreateNewPerformanceFile(void) { + if (nLastPerformance >= NUM_PERFORMANCES) { + // No space left for new performances + LOGWARN ("No space left for new performance"); + return false; + } + std::string sPerformanceName = NewPerformanceName; NewPerformanceName=""; nActualPerformance=nLastPerformance; @@ -829,19 +851,23 @@ bool CPerformanceConfig::ListPerformances() Result = f_findfirst (&Directory, &FileInfo, "SD:/" PERFORMANCE_DIR, "*.ini"); for (unsigned i = 0; Result == FR_OK && FileInfo.fname[0]; i++) { - if (!(FileInfo.fattrib & (AM_HID | AM_SYS))) - { - std::string FileName = FileInfo.fname; - size_t nLen = FileName.length(); - if ( nLen > 8 && nLen <26 && strcmp(FileName.substr(6,1).c_str(), "_")==0) - { - nPIndex=stoi(FileName.substr(0,6)); - if(nPIndex > nLastFileIndex) - { - nLastFileIndex=nPIndex; - } - - m_nPerformanceFileName[nLastPerformance++]= FileName; + if (nLastPerformance >= NUM_PERFORMANCES) { + LOGNOTE ("Skipping performance %s", FileInfo.fname); + } else { + if (!(FileInfo.fattrib & (AM_HID | AM_SYS))) + { + std::string FileName = FileInfo.fname; + size_t nLen = FileName.length(); + if ( nLen > 8 && nLen <26 && strcmp(FileName.substr(6,1).c_str(), "_")==0) + { + nPIndex=stoi(FileName.substr(0,6)); + if(nPIndex > nLastFileIndex) + { + nLastFileIndex=nPIndex; + } + + m_nPerformanceFileName[nLastPerformance++]= FileName; + } } } @@ -854,6 +880,8 @@ bool CPerformanceConfig::ListPerformances() } } + LOGNOTE ("Number of Performances: %d", nLastPerformance); + return nInternalFolderOk; } diff --git a/src/performanceconfig.h b/src/performanceconfig.h index ec32598..c151c7a 100644 --- a/src/performanceconfig.h +++ b/src/performanceconfig.h @@ -28,6 +28,7 @@ #include #define NUM_VOICE_PARAM 156 #define PERFORMANCE_DIR "performance" +#define NUM_PERFORMANCES 256 class CPerformanceConfig // Performance configuration { @@ -131,6 +132,7 @@ public: std::string GetNewPerformanceDefaultName(void); void SetNewPerformanceName(std::string nName); bool DeletePerformance(unsigned nID); + bool CheckFreePerformanceSlot(void); private: CPropertiesFatFsFile m_Properties; @@ -168,7 +170,7 @@ private: unsigned nLastFileIndex; unsigned nActualPerformance = 0; //unsigned nMenuSelectedPerformance = 0; - std::string m_nPerformanceFileName[1024]; + std::string m_nPerformanceFileName[NUM_PERFORMANCES]; FATFS *m_pFileSystem; bool nInternalFolderOk=false;