From 493586cbca340e631e7d68a3e8ade41016783d34 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Sun, 12 Nov 2017 07:09:36 -0800 Subject: [PATCH] Fix overflow check --- fmt/format.h | 20 +++++++++++--------- 1 file changed, 11 insertions(+), 9 deletions(-) diff --git a/fmt/format.h b/fmt/format.h index f28c467f..bd99746a 100644 --- a/fmt/format.h +++ b/fmt/format.h @@ -3758,17 +3758,19 @@ template unsigned parse_nonnegative_int(const Char *&s) { assert('0' <= *s && *s <= '9'); unsigned value = 0; - do { - unsigned new_value = value * 10 + (*s++ - '0'); - // Check if value wrapped around. - if (new_value < value) { - value = (std::numeric_limits::max)(); - break; - } - value = new_value; - } while ('0' <= *s && *s <= '9'); // Convert to unsigned to prevent a warning. unsigned max_int = (std::numeric_limits::max)(); + unsigned big = max_int / 10; + do { + // Check for overflow. + if (value > big) { + value = max_int + 1; + break; + } + value = value * 10 + (*s - '0'); + ++s; + } while ('0' <= *s && *s <= '9'); + // Convert to unsigned to prevent a warning. if (value > max_int) FMT_THROW(FormatError("number is too big")); return value;