fix: avoid overflow at postproc of the last loop

This commit is contained in:
ToruNiina
2023-02-12 18:50:46 +09:00
parent 418bfe9117
commit 51587338cd

View File

@@ -80,13 +80,22 @@ parse_binary_integer(location& loc)
integer retval(0), base(1); integer retval(0), base(1);
for(auto i(str.rbegin()), e(str.rend()); i!=e; ++i) for(auto i(str.rbegin()), e(str.rend()); i!=e; ++i)
{ {
assert(base != 0); // means overflow, checked in the above code
if(*i == '1') if(*i == '1')
{ {
retval += base; retval += base;
if(std::numeric_limits<integer>::max() / 2 < base)
{
base = 0;
}
base *= 2; base *= 2;
} }
else if(*i == '0') else if(*i == '0')
{ {
if(std::numeric_limits<integer>::max() / 2 < base)
{
base = 0;
}
base *= 2; base *= 2;
} }
else if(*i == '_') else if(*i == '_')