enable bugprone exception escape check

clang-13 includes new checks, and finds some issues.  The first is that
the boost::vector constructor can possibly throw, so replace the
underlying flat_map container with std::vector instead.

The others are places where we could possibly throw in destructors,
which would be bad.  Ideally we wouldn't use the destructor pattern, but
that would be non-trivial to clean up at this point, so just catch the
exception, and log it.  At the same time, catch exceptions thrown to
main and log them.

Tested: Code compiles

Signed-off-by: Ed Tanous <edtanous@google.com>
Change-Id: I77b86eaa2fc79e43d1ca044c78ca3b0ce0a7c38c
diff --git a/.clang-tidy b/.clang-tidy
index db950cb..3e9d87b 100644
--- a/.clang-tidy
+++ b/.clang-tidy
@@ -8,6 +8,7 @@
 bugprone-copy-constructor-init,
 bugprone-dangling-handle,
 bugprone-dynamic-static-initializers,
+bugprone-exception-escape,
 bugprone-fold-init-type,
 bugprone-forward-declaration-namespace,
 bugprone-forwarding-reference-overload,
diff --git a/include/dbus_monitor.hpp b/include/dbus_monitor.hpp
index a6c86c6..c8e46b7 100644
--- a/include/dbus_monitor.hpp
+++ b/include/dbus_monitor.hpp
@@ -19,7 +19,9 @@
 struct DbusWebsocketSession
 {
     std::vector<std::unique_ptr<sdbusplus::bus::match::match>> matches;
-    boost::container::flat_set<std::string> interfaces;
+    boost::container::flat_set<std::string, std::less<>,
+                               std::vector<std::string>>
+        interfaces;
 };
 
 static boost::container::flat_map<crow::websocket::Connection*,
@@ -110,14 +112,19 @@
         .onopen([&](crow::websocket::Connection& conn,
                     const std::shared_ptr<bmcweb::AsyncResp>&) {
             BMCWEB_LOG_DEBUG << "Connection " << &conn << " opened";
-            sessions[&conn] = DbusWebsocketSession();
+            sessions.try_emplace(&conn);
         })
         .onclose([&](crow::websocket::Connection& conn, const std::string&) {
             sessions.erase(&conn);
         })
         .onmessage([&](crow::websocket::Connection& conn,
                        const std::string& data, bool) {
-            DbusWebsocketSession& thisSession = sessions[&conn];
+            const auto sessionPair = sessions.find(&conn);
+            if (sessionPair == sessions.end())
+            {
+                conn.close("Internal error");
+            }
+            DbusWebsocketSession& thisSession = sessionPair->second;
             BMCWEB_LOG_DEBUG << "Connection " << &conn << " received " << data;
             nlohmann::json j = nlohmann::json::parse(data, nullptr, false);
             if (j.is_discarded())
diff --git a/include/openbmc_dbus_rest.hpp b/include/openbmc_dbus_rest.hpp
index 09bbd60..7f16ea8 100644
--- a/include/openbmc_dbus_rest.hpp
+++ b/include/openbmc_dbus_rest.hpp
@@ -227,7 +227,15 @@
 
     ~InProgressEnumerateData()
     {
-        findRemainingObjectsForEnumerate(objectPath, subtree, asyncResp);
+        try
+        {
+            findRemainingObjectsForEnumerate(objectPath, subtree, asyncResp);
+        }
+        catch (...)
+        {
+            BMCWEB_LOG_CRITICAL
+                << "findRemainingObjectsForEnumerate threw exception";
+        }
     }
 
     const std::string objectPath;
diff --git a/redfish-core/lib/managers.hpp b/redfish-core/lib/managers.hpp
index a48fd77..3a955cb 100644
--- a/redfish-core/lib/managers.hpp
+++ b/redfish-core/lib/managers.hpp
@@ -1480,7 +1480,7 @@
             "xyz.openbmc_project.ObjectMapper", "GetSubTree", "/", 0,
             std::array<const char*, 1>{thermalModeIface});
     }
-    ~SetPIDValues()
+    void pidSetDone()
     {
         if (asyncResp->res.result() != boost::beast::http::status::ok)
         {
@@ -1683,6 +1683,19 @@
             }
         }
     }
+
+    ~SetPIDValues()
+    {
+        try
+        {
+            pidSetDone();
+        }
+        catch (...)
+        {
+            BMCWEB_LOG_CRITICAL << "pidSetDone threw exception";
+        }
+    }
+
     std::shared_ptr<bmcweb::AsyncResp> asyncResp;
     std::vector<std::pair<std::string, std::optional<nlohmann::json>>>
         configuration;
diff --git a/src/webserver_main.cpp b/src/webserver_main.cpp
index bf98aae..b613008 100644
--- a/src/webserver_main.cpp
+++ b/src/webserver_main.cpp
@@ -61,7 +61,7 @@
     }
 }
 
-int main(int /*argc*/, char** /*argv*/)
+int run()
 {
     crow::Logger::setLogLevel(crow::LogLevel::Debug);
 
@@ -146,3 +146,16 @@
     crow::connections::systemBus.reset();
     return 0;
 }
+
+int main(int /*argc*/, char** /*argv*/)
+{
+    try
+    {
+        return run();
+    }
+    catch (...)
+    {
+        return -1;
+        BMCWEB_LOG_CRITICAL << "Threw exception to main";
+    }
+}