intrusionsensor: Fix a crash issue
The function monitorLanStatusChange() passes a copy of shared_ptr, and
the reference of the copy is passed into a lambda in match2(). When the
function returns, the lambda holds an invalidated reference, and crashes
when it is called.
There is another issue found during review that the matches are created
as static variables in the function, which will holds invalidated
reference if main() exits.
Fix the issues by changing the matches non-static and moving them into
main(), so that the variables' life cycles become correct.
Tested: Verify there is no crash when the lambda is called.
Signed-off-by: Lei YU <yulei.sh@bytedance.com>
Change-Id: Ie4555a3b7fc770ba552e42f14c99e2dabc91eaf8
diff --git a/src/IntrusionSensorMain.cpp b/src/IntrusionSensorMain.cpp
index e40c240..9ec3f5d 100644
--- a/src/IntrusionSensorMain.cpp
+++ b/src/IntrusionSensorMain.cpp
@@ -325,8 +325,12 @@
}
}
-static void
- monitorLanStatusChange(std::shared_ptr<sdbusplus::asio::connection> conn)
+/** @brief Initialize the lan status.
+ *
+ * @return true on success and false on failure
+ */
+static bool
+ initializeLanStatus(std::shared_ptr<sdbusplus::asio::connection>& conn)
{
// init lan port name from configuration
getNicNameInfo(conn);
@@ -336,7 +340,7 @@
if (!findFiles(fs::path("/sys/class/net/"), R"(eth\d+/ifindex)", files))
{
std::cerr << "No eth in system\n";
- return;
+ return false;
}
// iterate through all found eth files, and save ifindex
@@ -413,27 +417,7 @@
"org.freedesktop.DBus.Properties", "Get",
"org.freedesktop.network1.Link", "OperationalState");
}
-
- // add match to monitor lan status change
- static sdbusplus::bus::match::match match(
- static_cast<sdbusplus::bus::bus&>(*conn),
- "type='signal', member='PropertiesChanged',"
- "arg0namespace='org.freedesktop.network1.Link'",
- [](sdbusplus::message::message& msg) { processLanStatusChange(msg); });
-
- // add match to monitor entity manager signal about nic name config change
- static sdbusplus::bus::match::match match2(
- static_cast<sdbusplus::bus::bus&>(*conn),
- "type='signal', member='PropertiesChanged',path_namespace='" +
- std::string(inventoryPath) + "',arg0namespace='" + nicType + "'",
- [&conn](sdbusplus::message::message& msg) {
- if (msg.is_method_error())
- {
- std::cerr << "callback method error\n";
- return;
- }
- getNicNameInfo(conn);
- });
+ return true;
}
int main()
@@ -482,13 +466,39 @@
}
};
- auto match = std::make_unique<sdbusplus::bus::match::match>(
+ auto eventMatch = std::make_unique<sdbusplus::bus::match::match>(
static_cast<sdbusplus::bus::bus&>(*systemBus),
"type='signal',member='PropertiesChanged',path_namespace='" +
std::string(inventoryPath) + "',arg0namespace='" + sensorType + "'",
eventHandler);
- monitorLanStatusChange(systemBus);
+ if (initializeLanStatus(systemBus))
+ {
+ // add match to monitor lan status change
+ sdbusplus::bus::match::match lanStatusMatch(
+ static_cast<sdbusplus::bus::bus&>(*systemBus),
+ "type='signal', member='PropertiesChanged',"
+ "arg0namespace='org.freedesktop.network1.Link'",
+ [](sdbusplus::message::message& msg) {
+ processLanStatusChange(msg);
+ });
+
+ // add match to monitor entity manager signal about nic name config
+ // change
+ sdbusplus::bus::match::match lanConfigMatch(
+ static_cast<sdbusplus::bus::bus&>(*systemBus),
+ "type='signal', member='PropertiesChanged',path_namespace='" +
+ std::string(inventoryPath) + "',arg0namespace='" + nicType +
+ "'",
+ [&systemBus](sdbusplus::message::message& msg) {
+ if (msg.is_method_error())
+ {
+ std::cerr << "callback method error\n";
+ return;
+ }
+ getNicNameInfo(systemBus);
+ });
+ }
io.run();