Implemented error handling in cppcheck::Platform::loadFromXmlDocument(), enabled related unit test (#8409) (#4181)

This commit is contained in:
PKEuS 2022-06-08 12:35:23 +02:00 committed by GitHub
parent 86a8d88729
commit 44097b59ff
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 31 additions and 27 deletions

View File

@ -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;
}

View File

@ -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:
" </platform>";
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)