From faf09cd541d3aede2af962a8007bff2d903b04d0 Mon Sep 17 00:00:00 2001 From: cdozdil Date: Thu, 4 Jul 2024 09:13:50 +0300 Subject: [PATCH] prevent casting exceptions for dd2 --- OptiScaler/Config.cpp | 20 ++++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/OptiScaler/Config.cpp b/OptiScaler/Config.cpp index 291e0699..79c1e9ad 100644 --- a/OptiScaler/Config.cpp +++ b/OptiScaler/Config.cpp @@ -18,6 +18,11 @@ static inline bool isInteger(const std::string& str, int& value) { return (iss >> value) && iss.eof(); } +static inline bool isFloat(const std::string& str, float& value) { + std::istringstream iss(str); + return (iss >> value) && iss.eof(); +} + Config::Config(std::wstring fileName) { absoluteFileName = Util::DllPath().parent_path() / fileName;; @@ -499,9 +504,15 @@ std::optional Config::readString(std::string section, std::string k std::optional Config::readFloat(std::string section, std::string key) { auto value = readString(section, key); + try { - return std::stof(value.value()); + float result; + + if (value.has_value() && isFloat(value.value(), result)) + return result; + + return std::nullopt; } catch (const std::bad_optional_access&) // missing or auto value { @@ -522,7 +533,12 @@ std::optional Config::readInt(std::string section, std::string key) auto value = readString(section, key); try { - return std::stoi(value.value()); + int result; + + if (value.has_value() && isInteger(value.value(), result)) + return result; + + return std::nullopt; } catch (const std::bad_optional_access&) // missing or auto value {