From 44097b59ffeed296dbbc7e0278b7cab1666b6688 Mon Sep 17 00:00:00 2001 From: PKEuS <731902+PKEuS@users.noreply.github.com> Date: Wed, 8 Jun 2022 12:35:23 +0200 Subject: [PATCH] Implemented error handling in cppcheck::Platform::loadFromXmlDocument(), enabled related unit test (#8409) (#4181) --- lib/platform.cpp | 45 ++++++++++++++++++++++++++++--------------- test/testplatform.cpp | 13 ++----------- 2 files changed, 31 insertions(+), 27 deletions(-) diff --git a/lib/platform.cpp b/lib/platform.cpp index c45a8681d..35cc9f3dc 100644 --- a/lib/platform.cpp +++ b/lib/platform.cpp @@ -191,6 +191,14 @@ bool cppcheck::Platform::loadPlatformFile(const char exename[], const std::strin return loadFromXmlDocument(&doc); } +static unsigned int xmlTextAsUInt(const tinyxml2::XMLElement* node, bool& error) +{ + unsigned int retval = 0; + if (node->QueryUnsignedText(&retval) != tinyxml2::XML_SUCCESS) + error = true; + return retval; +} + bool cppcheck::Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc) { const tinyxml2::XMLElement * const rootnode = doc->FirstChildElement(); @@ -198,35 +206,40 @@ bool cppcheck::Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc) if (!rootnode || std::strcmp(rootnode->Name(), "platform") != 0) return false; + bool error = false; for (const tinyxml2::XMLElement *node = rootnode->FirstChildElement(); node; node = node->NextSiblingElement()) { - if (std::strcmp(node->Name(), "default-sign") == 0) - defaultSign = *node->GetText(); - else if (std::strcmp(node->Name(), "char_bit") == 0) - char_bit = std::atoi(node->GetText()); + if (std::strcmp(node->Name(), "default-sign") == 0) { + const char* str = node->GetText(); + if (str) + defaultSign = *str; + else + error = true; + } else if (std::strcmp(node->Name(), "char_bit") == 0) + char_bit = xmlTextAsUInt(node, error); else if (std::strcmp(node->Name(), "sizeof") == 0) { for (const tinyxml2::XMLElement *sz = node->FirstChildElement(); sz; sz = sz->NextSiblingElement()) { if (std::strcmp(sz->Name(), "short") == 0) - sizeof_short = std::atoi(sz->GetText()); + sizeof_short = xmlTextAsUInt(sz, error); else if (std::strcmp(sz->Name(), "bool") == 0) - sizeof_bool = std::atoi(sz->GetText()); + sizeof_bool = xmlTextAsUInt(sz, error); else if (std::strcmp(sz->Name(), "int") == 0) - sizeof_int = std::atoi(sz->GetText()); + sizeof_int = xmlTextAsUInt(sz, error); else if (std::strcmp(sz->Name(), "long") == 0) - sizeof_long = std::atoi(sz->GetText()); + sizeof_long = xmlTextAsUInt(sz, error); else if (std::strcmp(sz->Name(), "long-long") == 0) - sizeof_long_long = std::atoi(sz->GetText()); + sizeof_long_long = xmlTextAsUInt(sz, error); else if (std::strcmp(sz->Name(), "float") == 0) - sizeof_float = std::atoi(sz->GetText()); + sizeof_float = xmlTextAsUInt(sz, error); else if (std::strcmp(sz->Name(), "double") == 0) - sizeof_double = std::atoi(sz->GetText()); + sizeof_double = xmlTextAsUInt(sz, error); else if (std::strcmp(sz->Name(), "long-double") == 0) - sizeof_long_double = std::atoi(sz->GetText()); + sizeof_long_double = xmlTextAsUInt(sz, error); else if (std::strcmp(sz->Name(), "pointer") == 0) - sizeof_pointer = std::atoi(sz->GetText()); + sizeof_pointer = xmlTextAsUInt(sz, error); else if (std::strcmp(sz->Name(), "size_t") == 0) - sizeof_size_t = std::atoi(sz->GetText()); + sizeof_size_t = xmlTextAsUInt(sz, error); else if (std::strcmp(sz->Name(), "wchar_t") == 0) - sizeof_wchar_t = std::atoi(sz->GetText()); + sizeof_wchar_t = xmlTextAsUInt(sz, error); } } } @@ -237,5 +250,5 @@ bool cppcheck::Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc) long_long_bit = char_bit * sizeof_long_long; platformType = PlatformFile; - return true; + return !error; } diff --git a/test/testplatform.cpp b/test/testplatform.cpp index 0a86d727c..362dd2eb9 100644 --- a/test/testplatform.cpp +++ b/test/testplatform.cpp @@ -36,7 +36,7 @@ private: TEST_CASE(valid_config_file_3); TEST_CASE(valid_config_file_4); TEST_CASE(invalid_config_file_1); - // TEST_CASE(empty_elements); // TODO: Trac issue #8409 + TEST_CASE(empty_elements); } static bool readPlatform(cppcheck::Platform& platform, const char* xmldata) { @@ -286,7 +286,6 @@ private: ASSERT(!readPlatform(platform, xmldata)); } -#if 0 // @TODO: Enable when Trac issue #8409 has been fixed void empty_elements() { // Valid platform configuration without any usable information. // Similar like an empty file. @@ -310,18 +309,10 @@ private: " "; cppcheck::Platform platform; ASSERT(platform.platform(cppcheck::Platform::Win64)); - ASSERT(readPlatform(platform, xmldata)); + ASSERT(!readPlatform(platform, xmldata)); ASSERT_EQUALS(platform.PlatformFile, platform.platformType); ASSERT(!platform.isWindowsPlatform()); - ASSERT_EQUALS(8, platform.char_bit); - ASSERT_EQUALS('\0', platform.defaultSign); - ASSERT_EQUALS(1, platform.sizeof_bool); - ASSERT_EQUALS(2, platform.sizeof_short); - ASSERT_EQUALS(4, platform.sizeof_int); - ASSERT_EQUALS(4, platform.sizeof_long); - ASSERT_EQUALS(8, platform.sizeof_long_long); } -#endif }; REGISTER_TEST(TestPlatform)