From 3e5ecf1d2a8479fa40adc763fbf4c0baa0b2bcee Mon Sep 17 00:00:00 2001 From: Jarryd Beck Date: Fri, 14 Jun 2019 08:02:23 +1000 Subject: [PATCH] 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 | 1 + include/cxxopts.hpp | 4 ++-- 2 files changed, 3 insertions(+), 2 deletions(-) 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