Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue470 fix - Performance file bounds checking and error handling #494

Merged
merged 3 commits into from
May 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/minidexed.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
56 changes: 42 additions & 14 deletions src/performanceconfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,13 @@
// You should have received a copy of the GNU General Public License
// along with this program. If not, see <http://www.gnu.org/licenses/>.
//
#include <circle/logger.h>
#include "performanceconfig.h"
#include "mididevice.h"
#include <cstring>
#include <algorithm>
#include <algorithm>

LOGMODULE ("Performance");

CPerformanceConfig::CPerformanceConfig (FATFS *pFileSystem)
: m_Properties ("performance.ini", pFileSystem)
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
}
}

Expand All @@ -854,6 +880,8 @@ bool CPerformanceConfig::ListPerformances()
}
}

LOGNOTE ("Number of Performances: %d", nLastPerformance);

return nInternalFolderOk;
}

Expand Down
4 changes: 3 additions & 1 deletion src/performanceconfig.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include <Properties/propertiesfatfsfile.h>
#define NUM_VOICE_PARAM 156
#define PERFORMANCE_DIR "performance"
#define NUM_PERFORMANCES 256

class CPerformanceConfig // Performance configuration
{
Expand Down Expand Up @@ -131,6 +132,7 @@ class CPerformanceConfig // Performance configuration
std::string GetNewPerformanceDefaultName(void);
void SetNewPerformanceName(std::string nName);
bool DeletePerformance(unsigned nID);
bool CheckFreePerformanceSlot(void);

private:
CPropertiesFatFsFile m_Properties;
Expand Down Expand Up @@ -168,7 +170,7 @@ class CPerformanceConfig // Performance configuration
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;
Expand Down