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
29 changes: 26 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,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<std::uint8_t>::max();
}

bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc)
{
const tinyxml2::XMLElement * const rootnode = doc->FirstChildElement();
Expand All @@ -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<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 +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;
Expand Down
58 changes: 34 additions & 24 deletions test/testplatform.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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[] = "<?xml version=\"1.0\"?>\n"
"<platform>\n"
" <windows>true</windows>\n"
Expand All @@ -346,26 +348,34 @@ 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_1() const {
Expand Down
Loading