Commit 3e5ecf1d2a8479fa40adc763fbf4c0baa0b2bcee

Authored by Jarryd Beck
1 parent bd205738

Fix a couple of out of range errors

These were detected using -fsanitize=undefined parsing values equal to
INT_MAX and INT_MIN.
CHANGELOG.md
... ... @@ -23,6 +23,7 @@ options. The project adheres to semantic versioning.
23 23 * Throw on invalid option syntax when beginning with a `-`.
24 24 * Throw in `as` when option wasn't present.
25 25 * Fix catching exceptions by reference.
  26 +* Fix out of bounds errors parsing integers.
26 27  
27 28 ## 2.1.1
28 29  
... ...
include/cxxopts.hpp
... ... @@ -485,7 +485,7 @@ namespace cxxopts
485 485 {
486 486 if (negative)
487 487 {
488   - if (u > static_cast<U>(-(std::numeric_limits<T>::min)()))
  488 + if (u > -static_cast<U>((std::numeric_limits<T>::min)()))
489 489 {
490 490 throw argument_incorrect_type(text);
491 491 }
... ... @@ -523,7 +523,7 @@ namespace cxxopts
523 523 // if we got to here, then `t` is a positive number that fits into
524 524 // `R`. So to avoid MSVC C4146, we first cast it to `R`.
525 525 // See https://github.com/jarro2783/cxxopts/issues/62 for more details.
526   - return -static_cast<R>(t);
  526 + return -static_cast<R>(t-1)-1;
527 527 }
528 528  
529 529 template <typename R, typename T>
... ...