diff --git a/CHANGELOG.md b/CHANGELOG.md index a5c1d2d..014eab5 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -23,6 +23,7 @@ options. The project adheres to semantic versioning. * Throw on invalid option syntax when beginning with a `-`. * Throw in `as` when option wasn't present. * Fix catching exceptions by reference. +* Fix out of bounds errors parsing integers. ## 2.1.1 diff --git a/include/cxxopts.hpp b/include/cxxopts.hpp index 63d6336..aa27f8c 100644 --- a/include/cxxopts.hpp +++ b/include/cxxopts.hpp @@ -485,7 +485,7 @@ namespace cxxopts { if (negative) { - if (u > static_cast(-(std::numeric_limits::min)())) + if (u > -static_cast((std::numeric_limits::min)())) { 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 // `R`. So to avoid MSVC C4146, we first cast it to `R`. // See https://github.com/jarro2783/cxxopts/issues/62 for more details. - return -static_cast(t); + return -static_cast(t-1)-1; } template