Fix integer overflow and bad numeric parses

This commit is contained in:
Jason Turner 2015-10-04 08:53:22 -06:00
parent d2cf12f948
commit 14b3870efb

View File

@ -676,8 +676,9 @@ namespace chaiscript
#pragma GCC diagnostic pop #pragma GCC diagnostic pop
#endif #endif
} catch (const std::out_of_range &) { } catch (const std::out_of_range &) {
// too big to be signed
try {
auto u = std::stoull(val,nullptr,base); auto u = std::stoull(val,nullptr,base);
if (u >= std::numeric_limits<unsigned long>::min() && u <= std::numeric_limits<unsigned long>::max()) { if (u >= std::numeric_limits<unsigned long>::min() && u <= std::numeric_limits<unsigned long>::max()) {
@ -685,6 +686,10 @@ namespace chaiscript
} else { } else {
return const_var(static_cast<unsigned long long>(u)); return const_var(static_cast<unsigned long long>(u));
} }
} catch (const std::out_of_range &) {
// it's just simply too big
return const_var(std::numeric_limits<long long>::max());
}
} }
} }
@ -703,6 +708,7 @@ namespace chaiscript
} else { } else {
const auto start = m_position; const auto start = m_position;
if (m_position.has_more() && char_in_alphabet(*m_position, detail::float_alphabet) ) { if (m_position.has_more() && char_in_alphabet(*m_position, detail::float_alphabet) ) {
try {
if (Hex_()) { if (Hex_()) {
auto match = Position::str(start, m_position); auto match = Position::str(start, m_position);
auto bv = buildInt(16, match, true); auto bv = buildInt(16, match, true);
@ -737,6 +743,10 @@ namespace chaiscript
} }
return true; return true;
} }
} catch (const std::invalid_argument &) {
// error parsing number passed in to buildFloat/buildInt
return false;
}
} }
else { else {
return false; return false;