From 3639e1ca73204098a082872c2b01333d245779a7 Mon Sep 17 00:00:00 2001 From: nussaiba shaikh Date: Mon, 30 Nov 2026 15:18:01 +0530 Subject: [PATCH] validate char_bit and type sizes in Platform::loadFromXmlDocument --- lib/platform.cpp | 29 +++++++++++++++++++--- test/testplatform.cpp | 58 +++++++++++++++++++++++++------------------ 2 files changed, 60 insertions(+), 27 deletions(-) diff --git a/lib/platform.cpp b/lib/platform.cpp index b956b687419..46381b8acab 100644 --- a/lib/platform.cpp +++ b/lib/platform.cpp @@ -20,6 +20,8 @@ #include "path.h" +#include +#include #include #include #include @@ -324,6 +326,14 @@ 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. +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 +348,13 @@ 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); + 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 +385,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..2af4e40dd29 100644 --- a/test/testplatform.cpp +++ b/test/testplatform.cpp @@ -40,9 +40,10 @@ 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(empty_elements); TEST_CASE(default_platform); TEST_CASE(limitsDefines); @@ -323,9 +324,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 +348,34 @@ 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_1() const {