From fd443d91b2aa366adebc30e5fab751dfb1409791 Mon Sep 17 00:00:00 2001 From: Dale Whinham Date: Wed, 19 Jan 2022 12:21:10 +0000 Subject: [PATCH] Fix unterminated config file buffer --- CHANGELOG.md | 4 ++++ src/config.cpp | 8 +++++++- 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 2191b8e..1d6a332 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/src/config.cpp b/src/config.cpp index 41561af..3f32c8a 100644 --- a/src/config.cpp +++ b/src/config.cpp @@ -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(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)) \