From bcbe48b13f98b1666dd85a755cabcc09a672a41d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andreas=20J=C3=B6nsson?= Date: Sat, 9 May 2015 12:49:47 +0200 Subject: [PATCH] Make stoi_CountEmptyLinesAsMinimum more restrictive. It now only accepts digits and possibly a single minus character as the first character. Ordinary std::stoi has too much special magic, e.g. it converts "1 a" to "1". --- PowerEditor/src/MISC/Common/Common.cpp | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/PowerEditor/src/MISC/Common/Common.cpp b/PowerEditor/src/MISC/Common/Common.cpp index 61e30b922..f7b8cf5c2 100644 --- a/PowerEditor/src/MISC/Common/Common.cpp +++ b/PowerEditor/src/MISC/Common/Common.cpp @@ -757,6 +757,23 @@ int stoi_CountEmptyLinesAsMinimum(const generic_string &input) } else { + // Check minus characters. + const int minuses = std::count(input.begin(), input.end(), TEXT('-')); + if (minuses > 1) + { + throw std::invalid_argument("More than one minus sign."); + } + else if (minuses == 1 && input[0] != TEXT('-')) + { + throw std::invalid_argument("Minus sign must be first."); + } + + // Check for other characters which are not allowed. + if (input.find_first_not_of(TEXT("-0123456789")) != std::string::npos) + { + throw new std::invalid_argument("Invalid character found."); + } + return std::stoi(input); } }