diff --git a/lib/platform.cpp b/lib/platform.cpp index b956b687419..2448efd4040 100644 --- a/lib/platform.cpp +++ b/lib/platform.cpp @@ -20,6 +20,8 @@ #include "path.h" +#include +#include #include #include #include @@ -324,6 +326,16 @@ static unsigned int xmlTextAsBool(const tinyxml2::XMLElement* node, bool& error) return retval; } +// The *_bit members are std::uint8_t and are computed as char_bit * sizeof, so a +// product which does not fit is truncated - a width of 0 makes the shifts in +// min_value()/max_value() undefined. getLimitsDefines() also evaluates +// max_value(short_bit + 1)/max_value(int_bit + 1), where the parameter is a +// std::uint8_t, so the maximum width itself has to be rejected as well. +static bool bitWidthFits(unsigned int charBit, std::size_t size) +{ + return size != 0 && charBit * size < std::numeric_limits::max(); +} + bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc) { const tinyxml2::XMLElement * const rootnode = doc->FirstChildElement(); @@ -338,9 +350,15 @@ bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc) const char * const str = xmlText(node, error); if (!error) defaultSign = *str; - } else if (std::strcmp(name, "char_bit") == 0) - char_bit = xmlTextAsUInt(node, error); - else if (std::strcmp(name, "sizeof") == 0) { + } else if (std::strcmp(name, "char_bit") == 0) { + const unsigned int bits = xmlTextAsUInt(node, error); + // unsignedCharMax()/getLimitsDefines() pass char_bit + 1 to max_value(std::uint8_t), + // so the maximum value itself has to be rejected as well - it would wrap to 0 there. + if (bits == 0 || bits >= std::numeric_limits::max()) + error = true; + else + char_bit = bits; + } else if (std::strcmp(name, "sizeof") == 0) { for (const tinyxml2::XMLElement *sz = node->FirstChildElement(); sz; sz = sz->NextSiblingElement()) { const char* szname = sz->Name(); if (std::strcmp(szname, "short") == 0) @@ -371,6 +389,15 @@ bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc) windows = xmlTextAsBool(node, error); } } + // a size of 0 or a bit width which does not fit in the *_bit members would be + // used as-is in the analysis, so reject the file instead + if (sizeof_bool == 0 || sizeof_pointer == 0 || sizeof_size_t == 0 || sizeof_wchar_t == 0 || + !bitWidthFits(char_bit, sizeof_short) || !bitWidthFits(char_bit, sizeof_int) || + !bitWidthFits(char_bit, sizeof_long) || !bitWidthFits(char_bit, sizeof_long_long) || + !bitWidthFits(char_bit, sizeof_float) || !bitWidthFits(char_bit, sizeof_double) || + !bitWidthFits(char_bit, sizeof_long_double)) + error = true; + calculateBitMembers(); type = Type::File; return !error; diff --git a/test/testplatform.cpp b/test/testplatform.cpp index ccb7d9b41a8..bd8f8cfe64b 100644 --- a/test/testplatform.cpp +++ b/test/testplatform.cpp @@ -40,9 +40,13 @@ class TestPlatform : public TestFixture { // TODO: test native and unspecified TEST_CASE(valid_config_file_1); TEST_CASE(valid_config_file_2); - TEST_CASE(valid_config_file_4); TEST_CASE(invalid_config_file_1); TEST_CASE(invalid_config_file_2); + TEST_CASE(invalid_config_file_3); + TEST_CASE(invalid_config_file_4); + TEST_CASE(invalid_config_file_5); + TEST_CASE(invalid_config_file_6); + TEST_CASE(invalid_config_file_7); TEST_CASE(empty_elements); TEST_CASE(default_platform); TEST_CASE(limitsDefines); @@ -323,9 +327,10 @@ class TestPlatform : public TestFixture { TODO_ASSERT(!readPlatform(platform, xmldata)); } - void valid_config_file_4() const { - // Valid platform configuration with all possible values specified and - // set to 0. + void invalid_config_file_3() const { + // Platform configuration with all possible values specified and set to 0. + // char_bit and the type sizes are used to calculate the bit widths of the + // types, so a value of 0 would result in a bit width of 0. constexpr char xmldata[] = "\n" "\n" " true\n" @@ -346,26 +351,111 @@ class TestPlatform : public TestFixture { " \n" " "; PlatformTest platform; - ASSERT(readPlatform(platform, xmldata)); - ASSERT_EQUALS(Platform::Type::File, platform.type); - ASSERT(platform.isWindows()); - ASSERT_EQUALS(0, platform.char_bit); - ASSERT_EQUALS('z', platform.defaultSign); - ASSERT_EQUALS(0, platform.sizeof_bool); - ASSERT_EQUALS(0, platform.sizeof_short); - ASSERT_EQUALS(0, platform.sizeof_int); - ASSERT_EQUALS(0, platform.sizeof_long); - ASSERT_EQUALS(0, platform.sizeof_long_long); - ASSERT_EQUALS(0, platform.sizeof_float); - ASSERT_EQUALS(0, platform.sizeof_double); - ASSERT_EQUALS(0, platform.sizeof_long_double); - ASSERT_EQUALS(0, platform.sizeof_pointer); - ASSERT_EQUALS(0, platform.sizeof_size_t); - ASSERT_EQUALS(0, platform.sizeof_wchar_t); - ASSERT_EQUALS(0, platform.short_bit); - ASSERT_EQUALS(0, platform.int_bit); - ASSERT_EQUALS(0, platform.long_bit); - ASSERT_EQUALS(0, platform.long_long_bit); + ASSERT(!readPlatform(platform, xmldata)); + } + + void invalid_config_file_4() const { + // The bit widths are stored in std::uint8_t, so a char_bit and sizeof + // combination whose product does not fit is rejected. Here int_bit would + // be 32 * 8 == 256 which is truncated to 0. + constexpr char xmldata[] = "\n" + "\n" + " false\n" + " 32\n" + " signed\n" + " \n" + " 1\n" + " 2\n" + " 8\n" + " 8\n" + " 8\n" + " 4\n" + " 8\n" + " 8\n" + " 4\n" + " 4\n" + " 2\n" + " \n" + " "; + PlatformTest platform; + ASSERT(!readPlatform(platform, xmldata)); + } + + void invalid_config_file_5() const { + // unsignedCharMax()/getLimitsDefines() evaluate max_value(char_bit + 1), and the + // parameter is a std::uint8_t, so the largest char_bit wraps to a width of 0 there. + constexpr char xmldata[] = "\n" + "\n" + " false\n" + " 255\n" + " signed\n" + " \n" + " 1\n" + " 1\n" + " 1\n" + " 1\n" + " 1\n" + " 1\n" + " 1\n" + " 1\n" + " 1\n" + " 1\n" + " 1\n" + " \n" + " "; + PlatformTest platform; + ASSERT(!readPlatform(platform, xmldata)); + } + + void invalid_config_file_6() const { + // getLimitsDefines() evaluates max_value(short_bit + 1) for USHRT_MAX, and the + // parameter is a std::uint8_t, so a short width of 255 wraps to 0 there. + constexpr char xmldata[] = "\n" + "\n" + " false\n" + " 85\n" + " signed\n" + " \n" + " 1\n" + " 3\n" + " 1\n" + " 1\n" + " 1\n" + " 1\n" + " 1\n" + " 1\n" + " 1\n" + " 1\n" + " 1\n" + " \n" + " "; + PlatformTest platform; + ASSERT(!readPlatform(platform, xmldata)); + } + + void invalid_config_file_7() const { + // The same for max_value(int_bit + 1), which getLimitsDefines() uses for UINT_MAX. + constexpr char xmldata[] = "\n" + "\n" + " false\n" + " 1\n" + " signed\n" + " \n" + " 1\n" + " 1\n" + " 255\n" + " 255\n" + " 255\n" + " 1\n" + " 1\n" + " 1\n" + " 1\n" + " 1\n" + " 1\n" + " \n" + " "; + PlatformTest platform; + ASSERT(!readPlatform(platform, xmldata)); } void invalid_config_file_1() const {