Skip to content

Commit 4598048

Browse files
committed
reject the largest short/int widths as well: max_value(bit + 1) wraps to 0
1 parent b6a11cf commit 4598048

2 files changed

Lines changed: 57 additions & 2 deletions

File tree

‎lib/platform.cpp‎

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -328,10 +328,12 @@ static unsigned int xmlTextAsBool(const tinyxml2::XMLElement* node, bool& error)
328328

329329
// The *_bit members are std::uint8_t and are computed as char_bit * sizeof, so a
330330
// product which does not fit is truncated - a width of 0 makes the shifts in
331-
// min_value()/max_value() undefined.
331+
// min_value()/max_value() undefined. getLimitsDefines() also evaluates
332+
// max_value(short_bit + 1)/max_value(int_bit + 1), where the parameter is a
333+
// std::uint8_t, so the maximum width itself has to be rejected as well.
332334
static bool bitWidthFits(unsigned int charBit, std::size_t size)
333335
{
334-
return size != 0 && charBit * size <= std::numeric_limits<std::uint8_t>::max();
336+
return size != 0 && charBit * size < std::numeric_limits<std::uint8_t>::max();
335337
}
336338

337339
bool Platform::loadFromXmlDocument(const tinyxml2::XMLDocument *doc)

‎test/testplatform.cpp‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,8 @@ class TestPlatform : public TestFixture {
4545
TEST_CASE(invalid_config_file_3);
4646
TEST_CASE(invalid_config_file_4);
4747
TEST_CASE(invalid_config_file_5);
48+
TEST_CASE(invalid_config_file_6);
49+
TEST_CASE(invalid_config_file_7);
4850
TEST_CASE(empty_elements);
4951
TEST_CASE(default_platform);
5052
TEST_CASE(limitsDefines);
@@ -405,6 +407,57 @@ class TestPlatform : public TestFixture {
405407
ASSERT(!readPlatform(platform, xmldata));
406408
}
407409

410+
void invalid_config_file_6() const {
411+
// getLimitsDefines() evaluates max_value(short_bit + 1) for USHRT_MAX, and the
412+
// parameter is a std::uint8_t, so a short width of 255 wraps to 0 there.
413+
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"
414+
"<platform>\n"
415+
" <windows>false</windows>\n"
416+
" <char_bit>85</char_bit>\n"
417+
" <default-sign>signed</default-sign>\n"
418+
" <sizeof>\n"
419+
" <bool>1</bool>\n"
420+
" <short>3</short>\n"
421+
" <int>1</int>\n"
422+
" <long>1</long>\n"
423+
" <long-long>1</long-long>\n"
424+
" <float>1</float>\n"
425+
" <double>1</double>\n"
426+
" <long-double>1</long-double>\n"
427+
" <pointer>1</pointer>\n"
428+
" <size_t>1</size_t>\n"
429+
" <wchar_t>1</wchar_t>\n"
430+
" </sizeof>\n"
431+
" </platform>";
432+
PlatformTest platform;
433+
ASSERT(!readPlatform(platform, xmldata));
434+
}
435+
436+
void invalid_config_file_7() const {
437+
// The same for max_value(int_bit + 1), which getLimitsDefines() uses for UINT_MAX.
438+
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"
439+
"<platform>\n"
440+
" <windows>false</windows>\n"
441+
" <char_bit>1</char_bit>\n"
442+
" <default-sign>signed</default-sign>\n"
443+
" <sizeof>\n"
444+
" <bool>1</bool>\n"
445+
" <short>1</short>\n"
446+
" <int>255</int>\n"
447+
" <long>255</long>\n"
448+
" <long-long>255</long-long>\n"
449+
" <float>1</float>\n"
450+
" <double>1</double>\n"
451+
" <long-double>1</long-double>\n"
452+
" <pointer>1</pointer>\n"
453+
" <size_t>1</size_t>\n"
454+
" <wchar_t>1</wchar_t>\n"
455+
" </sizeof>\n"
456+
" </platform>";
457+
PlatformTest platform;
458+
ASSERT(!readPlatform(platform, xmldata));
459+
}
460+
408461
void invalid_config_file_1() const {
409462
// Invalid XML file: mismatching elements "boolt" vs "bool".
410463
constexpr char xmldata[] = "<?xml version=\"1.0\"?>\n"

0 commit comments

Comments
 (0)