0
0
mirror of https://github.com/zeux/pugixml.git synced 2024-12-28 06:10:55 +08:00

Fix integer overflow detection with leading zeros

Since they don't contribute to the resulting value just skip them before
parsing. This matches the behavior of strtol/strtoll and results in more
intuitive behavior.
This commit is contained in:
Arseny Kapoulkine 2015-10-17 10:33:50 -07:00
parent e8e54ea5de
commit 0e09571f21
2 changed files with 40 additions and 0 deletions

View File

@ -4350,6 +4350,10 @@ PUGI__NS_BEGIN
{
s += 2;
// since overflow detection relies on length of the sequence skip leading zeros
while (*s == '0')
s++;
const char_t* start = s;
for (;;)
@ -4370,6 +4374,10 @@ PUGI__NS_BEGIN
}
else
{
// since overflow detection relies on length of the sequence skip leading zeros
while (*s == '0')
s++;
const char_t* start = s;
for (;;)

View File

@ -1188,6 +1188,22 @@ TEST_XML(dom_as_uint_hex_overflow, "<node attr1='-0x1' attr2='0x100000000' attr3
CHECK(node.attribute(STR("attr3")).as_uint() == 4294967295);
}
TEST_XML(dom_as_int_many_digits, "<node attr1='0000000000000000000000000000000000000000000000001' />")
{
xml_node node = doc.child(STR("node"));
CHECK(node.attribute(STR("attr1")).as_int() == 1);
CHECK(node.attribute(STR("attr1")).as_uint() == 1);
}
TEST_XML(dom_as_int_hex_many_digits, "<node attr1='0x0000000000000000000000000000000000000000000000001' />")
{
xml_node node = doc.child(STR("node"));
CHECK(node.attribute(STR("attr1")).as_int() == 1);
CHECK(node.attribute(STR("attr1")).as_uint() == 1);
}
#ifdef PUGIXML_HAS_LONG_LONG
TEST_XML(dom_as_llong_overflow, "<node attr1='-9223372036854775809' attr2='9223372036854775808' attr3='-18446744073709551616' />")
{
@ -1224,6 +1240,22 @@ TEST_XML(dom_as_ullong_hex_overflow, "<node attr1='-0x1' attr2='0x10000000000000
CHECK(node.attribute(STR("attr2")).as_ullong() == 18446744073709551615ull);
CHECK(node.attribute(STR("attr3")).as_ullong() == 18446744073709551615ull);
}
TEST_XML(dom_as_llong_many_digits, "<node attr1='0000000000000000000000000000000000000000000000001' />")
{
xml_node node = doc.child(STR("node"));
CHECK(node.attribute(STR("attr1")).as_llong() == 1);
CHECK(node.attribute(STR("attr1")).as_ullong() == 1);
}
TEST_XML(dom_as_llong_hex_many_digits, "<node attr1='0x0000000000000000000000000000000000000000000000001' />")
{
xml_node node = doc.child(STR("node"));
CHECK(node.attribute(STR("attr1")).as_llong() == 1);
CHECK(node.attribute(STR("attr1")).as_ullong() == 1);
}
#endif
TEST_XML(dom_as_int_plus, "<node attr1='+1' attr2='+0xa' />")