From d800d8f00ef8e4b6a93912c9940344b3fcf46b65 Mon Sep 17 00:00:00 2001 From: Donat Zenichev Date: Wed, 18 Feb 2026 10:32:12 +0100 Subject: [PATCH] MT#64469 AmUtils: fix str2int (double) helper It's been noticed actually no result is written by the reference given to a funciton helper. Also it's not convenient with a rest of other helpers, which return either true/false on successfull or failed conversion, but just directly return the result of atof() as a bool, which is a mistake. Also add a comment to a function declaration. Change-Id: I4f18ce527638a3575ef7446a549650a951a0b583 --- core/AmUtils.cpp | 23 +++++++++++++++++++++-- core/AmUtils.h | 6 ++++++ 2 files changed, 27 insertions(+), 2 deletions(-) diff --git a/core/AmUtils.cpp b/core/AmUtils.cpp index 5a912cf4..9ced0464 100644 --- a/core/AmUtils.cpp +++ b/core/AmUtils.cpp @@ -269,8 +269,27 @@ bool str2int(const string& str, int& result) bool str2int(const string& str, double& result) { - char* s = (char*)str.c_str(); - return atof(s); + try + { + size_t pos; + result = std::stod(str, &pos); + + /* check whether the whole stirng was converted */ + if (pos != str.length()) + return false; + + return true; + } + catch (const std::invalid_argument) + { + return false; + } + catch (const std::out_of_range) + { + return false; + } + + return false; } bool str2int(char*& str, int& result, char sep) diff --git a/core/AmUtils.h b/core/AmUtils.h index 8267453d..fa6ceafa 100644 --- a/core/AmUtils.h +++ b/core/AmUtils.h @@ -143,6 +143,12 @@ bool str2int(char*& str, unsigned int& result, char sep = ' '); */ bool str2int(const string& str, int& result); +/** + * Convert a string to the double. + * @param str [in] string to convert. + * @param result [out] result integer. + * @return true on success + */ bool str2int(const string& str, double& result); /**