HwmonTempSensor: Fix a crash when an I3C device is scanned
When an I3C device is registered in hwmon sub-system, the device would have
a long address tag such as '0-4cc31020000', so if the scanning loop uses
'stoi' for the address parsing, this service crashs by this error.
terminate called after throwing an instance of 'std::out_of_range'
what(): stoi
Aborted (core dumped)
To prevent this issue, this commit replaces 'stoi' with std::from_chars
to uint64_t type since BasicVariantType can be up to uint64_t and it
actually parses unsigned numbers from Bus and Address settings.
Tested: HwmonTempSensor service didn't crash even when an I3C device is
enumerated in sysfs.
Signed-off-by: Jae Hyun Yoo <quic_jaehyoo@quicinc.com>
Change-Id: I5a06849b4149d535b2d4002f0f6e1e7bd5f94b97
diff --git a/src/HwmonTempMain.cpp b/src/HwmonTempMain.cpp
index bdb0ea2..8df8a8c 100644
--- a/src/HwmonTempMain.cpp
+++ b/src/HwmonTempMain.cpp
@@ -25,6 +25,7 @@
#include <sdbusplus/bus/match.hpp>
#include <array>
+#include <charconv>
#include <filesystem>
#include <fstream>
#include <functional>
@@ -307,14 +308,18 @@
std::string busStr = deviceName.substr(0, findHyphen);
std::string addrStr = deviceName.substr(findHyphen + 1);
- size_t bus = 0;
- size_t addr = 0;
- try
+ uint64_t bus = 0;
+ uint64_t addr = 0;
+ std::from_chars_result res;
+ res = std::from_chars(busStr.data(),
+ busStr.data() + busStr.size(), bus);
+ if (res.ec != std::errc{})
{
- bus = std::stoi(busStr);
- addr = std::stoi(addrStr, nullptr, 16);
+ continue;
}
- catch (const std::invalid_argument&)
+ res = std::from_chars(
+ addrStr.data(), addrStr.data() + addrStr.size(), addr, 16);
+ if (res.ec != std::errc{})
{
continue;
}