Utils: Fix big number string parsing error
Current configuration parsing logic can't handle big number strings so a
string is just left as a string without getting converted to an integer
and eventually it introduces crash issues in dbus-sensors because the
string can't be cast to an integer directly. This issue could be observed
when we add an I3C device which has 0x4cc31020000 as its Address setting
as an example.
To avoid this issue, this commit replaces 'stoul' with std::from_chars.
Note that std::from_chars is not smart enough to detect number base
automatically so it also adds '0x' prefix checking code.
Tested: Big numbers were parsed properly.
Signed-off-by: Jae Hyun Yoo <quic_jaehyoo@quicinc.com>
Change-Id: Ib7b74c5c21377895f25005e7e0edcf5f8ed6d1f2
diff --git a/src/Utils.cpp b/src/Utils.cpp
index 670ac2c..612e495 100644
--- a/src/Utils.cpp
+++ b/src/Utils.cpp
@@ -33,6 +33,7 @@
#include <valijson/schema_parser.hpp>
#include <valijson/validator.hpp>
+#include <charconv>
#include <filesystem>
#include <fstream>
#include <map>
@@ -330,19 +331,22 @@
return ret;
}
- try
+ std::string_view strView = *strPtr;
+ int base = 10;
+ if (boost::starts_with(strView, "0x"))
{
- size_t pos = 0;
- int64_t temp = std::stoul(*strPtr, &pos, 0);
- if (pos == strPtr->size())
- {
- keyPair.value() = static_cast<uint64_t>(temp);
- }
+ strView.remove_prefix(2);
+ base = 16;
}
- catch (const std::invalid_argument&)
- {}
- catch (const std::out_of_range&)
- {}
+
+ uint64_t temp = 0;
+ const char* strDataEndPtr = strView.data() + strView.size();
+ const std::from_chars_result res =
+ std::from_chars(strView.data(), strDataEndPtr, temp, base);
+ if (res.ec == std::errc{} && res.ptr == strDataEndPtr)
+ {
+ keyPair.value() = temp;
+ }
return ret;
}