Fix a couple of out of range errors

These were detected using -fsanitize=undefined parsing values equal to
INT_MAX and INT_MIN.
This commit is contained in:
Jarryd Beck 2019-06-14 08:02:23 +10:00
parent bd20573829
commit 3e5ecf1d2a
2 changed files with 3 additions and 2 deletions

View File

@ -23,6 +23,7 @@ options. The project adheres to semantic versioning.
* Throw on invalid option syntax when beginning with a `-`. * Throw on invalid option syntax when beginning with a `-`.
* Throw in `as` when option wasn't present. * Throw in `as` when option wasn't present.
* Fix catching exceptions by reference. * Fix catching exceptions by reference.
* Fix out of bounds errors parsing integers.
## 2.1.1 ## 2.1.1

View File

@ -485,7 +485,7 @@ namespace cxxopts
{ {
if (negative) if (negative)
{ {
if (u > static_cast<U>(-(std::numeric_limits<T>::min)())) if (u > -static_cast<U>((std::numeric_limits<T>::min)()))
{ {
throw argument_incorrect_type(text); throw argument_incorrect_type(text);
} }
@ -523,7 +523,7 @@ namespace cxxopts
// if we got to here, then `t` is a positive number that fits into // if we got to here, then `t` is a positive number that fits into
// `R`. So to avoid MSVC C4146, we first cast it to `R`. // `R`. So to avoid MSVC C4146, we first cast it to `R`.
// See https://github.com/jarro2783/cxxopts/issues/62 for more details. // See https://github.com/jarro2783/cxxopts/issues/62 for more details.
return -static_cast<R>(t); return -static_cast<R>(t-1)-1;
} }
template <typename R, typename T> template <typename R, typename T>