mgmt: prevent potential nullptr dereference in FaceManager

Change-Id: Ice622f2b51a3be448dc1c211dc4f80d967db14c6
Refs: #4548
diff --git a/daemon/mgmt/face-manager.cpp b/daemon/mgmt/face-manager.cpp
index ba7cb36..9d68256 100644
--- a/daemon/mgmt/face-manager.cpp
+++ b/daemon/mgmt/face-manager.cpp
@@ -472,18 +472,19 @@
 void
 FaceManager::connectFaceStateChangeSignal(const Face& face)
 {
+  using face::FaceState;
+
   FaceId faceId = face.getId();
   m_faceStateChangeConn[faceId] = face.afterStateChange.connect(
-    [this, faceId] (face::FaceState oldState, face::FaceState newState) {
-      const Face& face = *m_faceTable.get(faceId);
-
-      if (newState == face::FaceState::UP) {
+    [this, faceId, &face] (FaceState oldState, FaceState newState) {
+      if (newState == FaceState::UP) {
         notifyFaceEvent(face, ndn::nfd::FACE_EVENT_UP);
       }
-      else if (newState == face::FaceState::DOWN) {
+      else if (newState == FaceState::DOWN) {
         notifyFaceEvent(face, ndn::nfd::FACE_EVENT_DOWN);
       }
-      else if (newState == face::FaceState::CLOSED) {
+      else if (newState == FaceState::CLOSED) {
+        // cannot use face.getId() because it may already be reset to INVALID_FACEID
         m_faceStateChangeConn.erase(faceId);
       }
     });
diff --git a/daemon/mgmt/face-manager.hpp b/daemon/mgmt/face-manager.hpp
index 6f05b0f..ee5115a 100644
--- a/daemon/mgmt/face-manager.hpp
+++ b/daemon/mgmt/face-manager.hpp
@@ -74,8 +74,7 @@
                          const ndn::mgmt::CommandContinuation& done);
 
   static void
-  setLinkServiceOptions(Face& face,
-                        const ControlParameters& parameters);
+  setLinkServiceOptions(Face& face, const ControlParameters& parameters);
 
   static ControlParameters
   collectFaceProperties(const Face& face, bool wantUris);
@@ -94,7 +93,7 @@
              ndn::mgmt::StatusDatasetContext& context);
 
 private: // helpers for StatusDataset handler
-  bool
+  static bool
   matchFilter(const ndn::nfd::FaceQueryFilter& filter, const Face& face);
 
   /** \brief get status of face, including properties and counters
@@ -119,14 +118,12 @@
 private:
   FaceSystem& m_faceSystem;
   FaceTable& m_faceTable;
-
-PUBLIC_WITH_TESTS_ELSE_PRIVATE:
-  std::map<FaceId, signal::ScopedConnection> m_faceStateChangeConn;
-
-private:
   ndn::mgmt::PostNotification m_postNotification;
   signal::ScopedConnection m_faceAddConn;
   signal::ScopedConnection m_faceRemoveConn;
+
+PUBLIC_WITH_TESTS_ELSE_PRIVATE:
+  std::map<FaceId, signal::ScopedConnection> m_faceStateChangeConn;
 };
 
 } // namespace nfd