From 1dd915ccdf361fa16161ebff859e17f459f0c617 Mon Sep 17 00:00:00 2001 From: Filippos Karapetis Date: Tue, 20 Aug 2019 11:38:27 +0300 Subject: COMMON: Handle invalid INI file characters gracefully This replaces all the asserts with warning messages --- common/ini-file.cpp | 85 ++++++++++++++++++++++++++++++++++++++++------------- 1 file changed, 64 insertions(+), 21 deletions(-) (limited to 'common/ini-file.cpp') diff --git a/common/ini-file.cpp b/common/ini-file.cpp index d35c1e8d16..90d40b7c7d 100644 --- a/common/ini-file.cpp +++ b/common/ini-file.cpp @@ -28,19 +28,13 @@ namespace Common { -bool INIFile::isValidName(const String &name) { +bool INIFile::isValidName(const String &name) const { const char *p = name.c_str(); while (*p && (isAlnum(*p) || *p == '-' || *p == '_' || *p == '.' || *p == ' ')) p++; return *p == 0; } -INIFile::INIFile() { -} - -INIFile::~INIFile() { -} - void INIFile::clear() { _sections.clear(); } @@ -125,7 +119,10 @@ bool INIFile::loadFromStream(SeekableReadStream &stream) { section.comment = comment; comment.clear(); - assert(isValidName(section.name)); + if (!isValidName(section.name)) { + warning("Invalid section name: %s", section.name.c_str()); + return false; + } } else { // This line should be a line with a 'key=value' pair, or an empty one. @@ -163,7 +160,10 @@ bool INIFile::loadFromStream(SeekableReadStream &stream) { kv.comment = comment; comment.clear(); - assert(isValidName(kv.key)); + if (!isValidName(kv.key)) { + warning("Invalid key name: %s", kv.key.c_str()); + return false; + } section.keys.push_back(kv); } @@ -240,7 +240,11 @@ void INIFile::addSection(const String §ion) { } void INIFile::removeSection(const String §ion) { - assert(isValidName(section)); + if (!isValidName(section)) { + warning("Invalid section name: %s", section.c_str()); + return; + } + for (List
::iterator i = _sections.begin(); i != _sections.end(); ++i) { if (section.equalsIgnoreCase(i->name)) { _sections.erase(i); @@ -250,14 +254,25 @@ void INIFile::removeSection(const String §ion) { } bool INIFile::hasSection(const String §ion) const { - assert(isValidName(section)); + if (!isValidName(section)) { + warning("Invalid section name: %s", section.c_str()); + return false; + } + const Section *s = getSection(section); return s != nullptr; } void INIFile::renameSection(const String &oldName, const String &newName) { - assert(isValidName(oldName)); - assert(isValidName(newName)); + if (!isValidName(oldName)) { + warning("Invalid section name: %s", oldName.c_str()); + return; + } + + if (!isValidName(newName)) { + warning("Invalid section name: %s", newName.c_str()); + return; + } Section *os = getSection(oldName); const Section *ns = getSection(newName); @@ -278,8 +293,15 @@ void INIFile::renameSection(const String &oldName, const String &newName) { bool INIFile::hasKey(const String &key, const String §ion) const { - assert(isValidName(key)); - assert(isValidName(section)); + if (!isValidName(key)) { + warning("Invalid key name: %s", key.c_str()); + return false; + } + + if (!isValidName(section)) { + warning("Invalid section name: %s", section.c_str()); + return false; + } const Section *s = getSection(section); if (!s) @@ -288,8 +310,15 @@ bool INIFile::hasKey(const String &key, const String §ion) const { } void INIFile::removeKey(const String &key, const String §ion) { - assert(isValidName(key)); - assert(isValidName(section)); + if (!isValidName(key)) { + warning("Invalid key name: %s", key.c_str()); + return; + } + + if (!isValidName(section)) { + warning("Invalid section name: %s", section.c_str()); + return; + } Section *s = getSection(section); if (s) @@ -297,9 +326,15 @@ void INIFile::removeKey(const String &key, const String §ion) { } bool INIFile::getKey(const String &key, const String §ion, String &value) const { - assert(isValidName(key)); - assert(isValidName(section)); + if (!isValidName(key)) { + warning("Invalid key name: %s", key.c_str()); + return false; + } + if (!isValidName(section)) { + warning("Invalid section name: %s", section.c_str()); + return false; + } const Section *s = getSection(section); if (!s) return false; @@ -311,8 +346,16 @@ bool INIFile::getKey(const String &key, const String §ion, String &value) co } void INIFile::setKey(const String &key, const String §ion, const String &value) { - assert(isValidName(key)); - assert(isValidName(section)); + if (!isValidName(key)) { + warning("Invalid key name: %s", key.c_str()); + return; + } + + if (!isValidName(section)) { + warning("Invalid section name: %s", section.c_str()); + return; + } + // TODO: Verify that value is valid, too. In particular, it shouldn't // contain CR or LF... -- cgit v1.2.3