fix: ignore whitespace around config keys and values

"client_location = ../client " or a value with a trailing space is a
common setup mistake that surfaces much later as an unrelated error (a
missing client folder, a failed database login). Keys and values are now
trimmed of spaces, tabs and line endings when the ini is read, which
also covers the \r of files saved with Windows line endings. Spaces
inside a value are kept, and lines with an empty key are ignored.

Verified with a new unit test that loads an ini with padded keys and
values.

Refs #1113

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
Aaron Kimbrell
2026-09-26 18:42:13 -05:00
parent d671d26635
commit 040d7ec067
2 changed files with 38 additions and 5 deletions

View File

@@ -4,6 +4,10 @@
#include "dConfig.h"
#include "ConfigSync.h"
#include "BinaryPathFinder.h"
#include <filesystem>
#include <fstream>
namespace {
void SetEnv(const char* name, const char* value) {
@@ -111,3 +115,24 @@ TEST(ConfigLayerTests, OwnedValuesComeFromTheOwner) {
EXPECT_EQ(config.GetValue("permission_accounts_ban"), "6");
EXPECT_EQ(config.GetValue("permission_players_view"), "2");
}
TEST(ConfigLayerTests, WhitespaceAroundKeysAndValuesIsIgnored) {
const auto path = BinaryPathFinder::GetBinaryDir() / "whitespace_test_config.ini";
{
std::ofstream out(path, std::ios::binary);
out << "zz_plain=value\n";
out << "zz_spaced = spaced value \r\n";
out << "\tzz_tabbed\t=\tx\t\n";
out << " = no key\n";
out << "zz_empty= \n";
}
dConfig config("whitespace_test_config.ini");
EXPECT_EQ(config.GetValue("zz_plain"), "value");
EXPECT_EQ(config.GetValue("zz_spaced"), "spaced value"); // inner spaces are kept
EXPECT_EQ(config.GetValue("zz_tabbed"), "x");
EXPECT_EQ(config.GetValue("zz_empty"), "");
EXPECT_EQ(config.GetValue(""), "");
std::filesystem::remove(path);
}