#gpowerd Replace `std::move` with `std::exchange(..., nullptr)` for AnyInvocable
It is not guaranteed that `my_invocable` is empty after `std::move(my_invocable)`: https://screenshot.googleplex.com/4GXk4KEkrhJQfYn
This change ensures that AnyInvocable pointers are explicitly emptied after their ownership is transferred.
PiperOrigin-RevId: 839767009
Change-Id: Ic0ed00648b52e8f8413993e7ec8a91149de94905
diff --git a/action_context.cc b/action_context.cc
index a21e688..6cdb554 100644
--- a/action_context.cc
+++ b/action_context.cc
@@ -228,7 +228,7 @@
case safepower_agent_proto::ACTION_STATE_SUCCESS:
case safepower_agent_proto::ACTION_STATE_ERROR:
status = DaemonContext::Get().scheduler().DelayCall(
- [this, action_impl = std::move(action_impl_)]() mutable {
+ [this, action_impl = std::exchange(action_impl_, nullptr)]() mutable {
Finish(std::move(action_impl));
},
absl::ZeroDuration(), execution_task_name());
diff --git a/callback_manager.cc b/callback_manager.cc
index d3de37b..bc4aaba 100644
--- a/callback_manager.cc
+++ b/callback_manager.cc
@@ -90,7 +90,7 @@
absl::MutexLock lock(mutex_);
for (auto& callback : callbacks_) {
if (callback) {
- callbacks_to_run.push_back(std::move(callback));
+ callbacks_to_run.push_back(std::exchange(callback, nullptr));
}
}
}
diff --git a/condition.cc b/condition.cc
index d1545b4..104744b 100644
--- a/condition.cc
+++ b/condition.cc
@@ -293,7 +293,8 @@
return absl::FailedPreconditionError("Condition is not active");
}
return DaemonContext::Get().scheduler().DelayCall(
- [callback = std::move(callback_), match_status = std::move(match_status),
+ [callback = std::exchange(callback_, nullptr),
+ match_status = std::move(match_status),
match_list = std::move(match_list)]() mutable {
std::move(callback)(std::move(match_status), std::move(match_list));
},