From 3639e1ca73204098a082872c2b01333d245779a7 Mon Sep 17 00:00:00 2001 From: nussaiba shaikh Date: Mon, 30 Nov 2026 15:18:01 +0530 Subject: [PATCH 1/3] 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 { From b6a11cf5609fa46a48a24a4be8e0d7e385f93e55 Mon Sep 17 00:00:00 2001 From: nussaiba shaikh Date: Fri, 4 Dec 2026 21:24:02 +0530 Subject: [PATCH 2/3] reject the largest char_bit as well: max_value(char_bit + 1) wraps to 0 --- lib/platform.cpp | 4 +++- test/testplatform.cpp | 27 +++++++++++++++++++++++++++ 2 files changed, 30 insertions(+), 1 deletion(-) diff --git a/lib/platform.cpp b/lib/platform.cpp index 46381b8acab..69a5a79cce8 100644 --- a/lib/platform.cpp +++ b/lib/platform.cpp @@ -350,7 +350,9 @@ bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc) defaultSign = *str; } else if (std::strcmp(name, "char_bit") == 0) { const unsigned int bits = xmlTextAsUInt(node, error); - if (bits == 0 || bits > std::numeric_limits::max()) + // 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; diff --git a/test/testplatform.cpp b/test/testplatform.cpp index 2af4e40dd29..9c4ed49bc4e 100644 --- a/test/testplatform.cpp +++ b/test/testplatform.cpp @@ -44,6 +44,7 @@ class TestPlatform : public TestFixture { 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(empty_elements); TEST_CASE(default_platform); TEST_CASE(limitsDefines); @@ -378,6 +379,32 @@ class TestPlatform : public TestFixture { 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_1() const { // Invalid XML file: mismatching elements "boolt" vs "bool". constexpr char xmldata[] = "\n" From 45980480a2cf1433ba7c05cf5b7ace0da7923977 Mon Sep 17 00:00:00 2001 From: nussaiba shaikh Date: Sat, 5 Dec 2026 14:24:31 +0530 Subject: [PATCH 3/3] reject the largest short/int widths as well: max_value(bit + 1) wraps to 0 --- lib/platform.cpp | 6 +++-- test/testplatform.cpp | 53 +++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 57 insertions(+), 2 deletions(-) diff --git a/lib/platform.cpp b/lib/platform.cpp index 69a5a79cce8..2448efd4040 100644 --- a/lib/platform.cpp +++ b/lib/platform.cpp @@ -328,10 +328,12 @@ static unsigned int xmlTextAsBool(const tinyxml2::XMLElement* node, bool& error) // 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. +// 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(); + return size != 0 && charBit * size < std::numeric_limits::max(); } bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc) diff --git a/test/testplatform.cpp b/test/testplatform.cpp index 9c4ed49bc4e..bd8f8cfe64b 100644 --- a/test/testplatform.cpp +++ b/test/testplatform.cpp @@ -45,6 +45,8 @@ class TestPlatform : public TestFixture { 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); @@ -405,6 +407,57 @@ class TestPlatform : public TestFixture { 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 { // Invalid XML file: mismatching elements "boolt" vs "bool". constexpr char xmldata[] = "\n"