From 58f947ffff3b5a6376c535322a8faa445c6834a4 Mon Sep 17 00:00:00 2001 From: diyelectromusic <68612569+diyelectromusic@users.noreply.github.com> Date: Thu, 6 Apr 2023 10:19:55 +0100 Subject: [PATCH] Limit nesting of subdirectories to open --- src/sysexfileloader.cpp | 14 ++++++++++---- src/sysexfileloader.h | 3 ++- 2 files changed, 12 insertions(+), 5 deletions(-) diff --git a/src/sysexfileloader.cpp b/src/sysexfileloader.cpp index af1f244..1088966 100644 --- a/src/sysexfileloader.cpp +++ b/src/sysexfileloader.cpp @@ -97,18 +97,18 @@ void CSysExFileLoader::Load (bool bHeaderlessSysExVoices) dirent *pEntry; while ((pEntry = readdir (pDirectory)) != nullptr) { - LoadBank(m_DirName.c_str (), pEntry->d_name, bHeaderlessSysExVoices); + LoadBank(m_DirName.c_str (), pEntry->d_name, bHeaderlessSysExVoices, 0); } LOGDBG ("%u Banks loaded. Highest Bank loaded: #%u", m_nBanksLoaded, m_nNumHighestBank); closedir (pDirectory); } -void CSysExFileLoader::LoadBank (const char * sDirName, const char * sBankName, bool bHeaderlessSysExVoices) +void CSysExFileLoader::LoadBank (const char * sDirName, const char * sBankName, bool bHeaderlessSysExVoices, unsigned nSubDirCount) { unsigned nBank; size_t nLen = strlen (sBankName); - + if ( nLen < 5 // "[NNNN]N[_name].syx" || strcasecmp (&sBankName[nLen-4], ".syx") != 0 || sscanf (sBankName, "%u", &nBank) != 1) @@ -121,12 +121,18 @@ void CSysExFileLoader::LoadBank (const char * sDirName, const char * sBankName, DIR *pDirectory = opendir (Dirname.c_str ()); if (pDirectory) { + if (nSubDirCount >= MaxSubDirs) + { + LOGWARN ("Too many nested subdirectories: %s", sBankName); + return; + } + LOGDBG ("Processing subdirectory %s", sBankName); dirent *pEntry; while ((pEntry = readdir (pDirectory)) != nullptr) { - LoadBank(Dirname.c_str (), pEntry->d_name, bHeaderlessSysExVoices); + LoadBank(Dirname.c_str (), pEntry->d_name, bHeaderlessSysExVoices, nSubDirCount+1); } closedir (pDirectory); } diff --git a/src/sysexfileloader.h b/src/sysexfileloader.h index 2cfb2ce..4918db6 100644 --- a/src/sysexfileloader.h +++ b/src/sysexfileloader.h @@ -35,6 +35,7 @@ public: static const size_t SizeSingleVoice = 156; static const unsigned VoiceSysExHdrSize = 8; // Additional (optional) Header/Footer bytes for bank of 32 voices static const unsigned VoiceSysExSize = 4096; // Bank of 32 voices as per DX7 MIDI Spec + static const unsigned MaxSubDirs = 3; // Number of nested subdirectories supported. struct TVoiceBank { @@ -82,7 +83,7 @@ private: static uint8_t s_DefaultVoice[SizeSingleVoice]; - void LoadBank (const char * sDirName, const char * sBankName, bool bHeaderlessSysExVoices); + void LoadBank (const char * sDirName, const char * sBankName, bool bHeaderlessSysExVoices, unsigned nSubDirCount); }; #endif