clang-tidy: Fix Dangling Pointer Warning
The following error was reported during clang-tidy enablement due to
a temporary object being destroyed at the end of the full-expression,
leaving a dangling pointer. This fix addresses the issue by changing
str to a proper string instead of a string_view.
'''
0m../apphandler.cpp:545:36: error: object backing the pointer will be destroyed at the end of the full-expression [-Werror
0m../apphandler.cpp:564:40: error: object backing the pointer will be destroyed at the end of the full-expression [-Werror
1m../apphandler.cpp:523:36: error: object backing the pointer will be destroyed at the end of the full-expression [-Werror
'''
Tested: Build and unit testing verified.
Change-Id: I4f8f2dc3f24a4e417e6805075cfaefddb111efaa
Signed-off-by: Jayanth Othayoth <ojayanth@gmail.com>
diff --git a/apphandler.cpp b/apphandler.cpp
index 3af8b3d..1e32061 100644
--- a/apphandler.cpp
+++ b/apphandler.cpp
@@ -520,9 +520,10 @@
// convert major
{
- std::string_view str = m[matches[0]].str();
- auto [ptr, ec]{std::from_chars(str.begin(), str.end(), val)};
- if (ec != std::errc() || ptr != str.begin() + str.size())
+ std::string str = m[matches[0]].str();
+ const auto& [ptr, ec] =
+ std::from_chars(str.data(), str.data() + str.size(), val);
+ if (ec != std::errc() || ptr != str.data() + str.size())
{ // failed to convert major string
return -1;
}
@@ -542,9 +543,10 @@
// convert minor
{
- std::string_view str = m[matches[1]].str();
- auto [ptr, ec]{std::from_chars(str.begin(), str.end(), val)};
- if (ec != std::errc() || ptr != str.begin() + str.size())
+ std::string str = m[matches[1]].str();
+ const auto& [ptr, ec] =
+ std::from_chars(str.data(), str.data() + str.size(), val);
+ if (ec != std::errc() || ptr != str.data() + str.size())
{ // failed to convert minor string
return -1;
}
@@ -561,10 +563,11 @@
continue;
}
- std::string_view str = m[matches[i + 2]].str();
+ std::string str = m[matches[i + 2]].str();
+ const char* cstr = str.c_str();
auto [ptr,
- ec]{std::from_chars(str.begin(), str.end(), val, 16)};
- if (ec != std::errc() || ptr != str.begin() + str.size())
+ ec] = std::from_chars(cstr, cstr + str.size(), val, 16);
+ if (ec != std::errc() || ptr != cstr + str.size())
{ // failed to convert aux byte string
break;
}