Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 30 additions & 3 deletions lib/platform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@

#include "path.h"

#include <cstddef>
#include <cstdint>
#include <cstring>
#include <iostream>
#include <limits>
Expand Down Expand Up @@ -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<std::uint8_t>::max();
}

bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc)
{
const tinyxml2::XMLElement * const rootnode = doc->FirstChildElement();
Expand All @@ -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<std::uint8_t>::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)
Expand Down Expand Up @@ -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();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

AI review

When validation fails, only error is set. The sizes read from the file stay in the object, calculateBitMembers() still runs, and type is still set to Type::File. A rejected file therefore leaves e.g. int_bit == 0 (or a truncated width such as long_bit == 64 for 8 * 40) in the Platform.

The CLI exits on the false return, so it is safe. gui/mainwindow.cpp:1188 calls settings.platform.loadFromFile(paths, platform.toStdString()); and ignores the result, so a GUI project that names such a platform file still analyses with the bogus widths. That is the 1LL << (bit - 1) UB this PR is meant to remove.

Repro: load a file with <int>0</int> through Platform::loadFromFile → returns false, but type=platformFile int_bit=0.

Two possible fixes: validate before assigning anything and leave *this unchanged on error (load into a temporary Platform and assign it only on success), or check the return value in the GUI. A test that asserts the platform is unchanged after a rejected file would cover this.

type = Type::File;
return !error;
Expand Down
138 changes: 114 additions & 24 deletions test/testplatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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[] = "<?xml version=\"1.0\"?>\n"
"<platform>\n"
" <windows>true</windows>\n"
Expand All @@ -346,26 +351,111 @@ class TestPlatform : public TestFixture {
" </sizeof>\n"
" </platform>";
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[] = "<?xml version=\"1.0\"?>\n"
"<platform>\n"
" <windows>false</windows>\n"
" <char_bit>32</char_bit>\n"
" <default-sign>signed</default-sign>\n"
" <sizeof>\n"
" <bool>1</bool>\n"
" <short>2</short>\n"
" <int>8</int>\n"
" <long>8</long>\n"
" <long-long>8</long-long>\n"
" <float>4</float>\n"
" <double>8</double>\n"
" <long-double>8</long-double>\n"
" <pointer>4</pointer>\n"
" <size_t>4</size_t>\n"
" <wchar_t>2</wchar_t>\n"
" </sizeof>\n"
" </platform>";
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[] = "<?xml version=\"1.0\"?>\n"
"<platform>\n"
" <windows>false</windows>\n"
" <char_bit>255</char_bit>\n"
" <default-sign>signed</default-sign>\n"
" <sizeof>\n"
" <bool>1</bool>\n"
" <short>1</short>\n"
" <int>1</int>\n"
" <long>1</long>\n"
" <long-long>1</long-long>\n"
" <float>1</float>\n"
" <double>1</double>\n"
" <long-double>1</long-double>\n"
" <pointer>1</pointer>\n"
" <size_t>1</size_t>\n"
" <wchar_t>1</wchar_t>\n"
" </sizeof>\n"
" </platform>";
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[] = "<?xml version=\"1.0\"?>\n"
"<platform>\n"
" <windows>false</windows>\n"
" <char_bit>85</char_bit>\n"
" <default-sign>signed</default-sign>\n"
" <sizeof>\n"
" <bool>1</bool>\n"
" <short>3</short>\n"
" <int>1</int>\n"
" <long>1</long>\n"
" <long-long>1</long-long>\n"
" <float>1</float>\n"
" <double>1</double>\n"
" <long-double>1</long-double>\n"
" <pointer>1</pointer>\n"
" <size_t>1</size_t>\n"
" <wchar_t>1</wchar_t>\n"
" </sizeof>\n"
" </platform>";
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[] = "<?xml version=\"1.0\"?>\n"
"<platform>\n"
" <windows>false</windows>\n"
" <char_bit>1</char_bit>\n"
" <default-sign>signed</default-sign>\n"
" <sizeof>\n"
" <bool>1</bool>\n"
" <short>1</short>\n"
" <int>255</int>\n"
" <long>255</long>\n"
" <long-long>255</long-long>\n"
" <float>1</float>\n"
" <double>1</double>\n"
" <long-double>1</long-double>\n"
" <pointer>1</pointer>\n"
" <size_t>1</size_t>\n"
" <wchar_t>1</wchar_t>\n"
" </sizeof>\n"
" </platform>";
PlatformTest platform;
ASSERT(!readPlatform(platform, xmldata));
}

void invalid_config_file_1() const {
Expand Down
Loading