Fix unterminated config file buffer

This commit is contained in:
Dale Whinham
2022-01-19 12:21:10 +00:00
parent 07004eac54
commit fd443d91b2
2 changed files with 11 additions and 1 deletions

View File

@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## [Unreleased]
### Fixed
- A bug in the config file reader (unterminated string) could cause the last entry in the file to be read as a corrupted value if the file ended without a newline.
## [0.11.0] - 2021-12-12
### Added

View File

@@ -89,8 +89,9 @@ bool CConfig::Initialize(const char* pPath)
return false;
}
// +1 byte for null terminator
const UINT nSize = f_size(&File);
char Buffer[nSize];
char Buffer[nSize + 1];
UINT nRead;
if (f_read(&File, Buffer, nSize, &nRead) != FR_OK)
@@ -100,6 +101,9 @@ bool CConfig::Initialize(const char* pPath)
return false;
}
// Ensure null-terminated
Buffer[nRead] = '\0';
const int nResult = ini_parse_string(Buffer, INIHandler, this);
if (nResult > 0)
CLogger::Get()->Write(ConfigName, LogWarning, "Config parse error on line %d", nResult);
@@ -162,6 +166,8 @@ int CConfig::INIHandler(void* pUser, const char* pSection, const char* pName, co
CConfig* const pConfig = static_cast<CConfig*>(pUser);
size_t nFXProfileIndex;
//CLogger::Get()->Write(ConfigName, LogDebug, "'%s', '%s', '%s'", pSection, pName, pValue);
// Automatically generate ParseOption() calls from macro definition file
#define BEGIN_SECTION(SECTION) \
if (!strcmp(#SECTION, pSection)) \