attn PEL additional-data map access cleanup

Signed-off-by: austinfcui <austinfcui@gmail.com>
Change-Id: Ic5b167ee45a31bc16bb5d30e95dced2a72818e77
diff --git a/attn/attn_logging.cpp b/attn/attn_logging.cpp
index a88a85e..3141130 100644
--- a/attn/attn_logging.cpp
+++ b/attn/attn_logging.cpp
@@ -108,14 +108,28 @@
 
     // The additional data contains the TI info as well as the value for the
     // subystem that provided the TI info. Get the subystem from additional
-    // data and then populate the prmary SRC and SRC words for the custom PEL
-    // based on the sybsystem's TI info.
-    uint8_t subsystem = std::stoi(i_additional["Subsystem"]);
-    tiPel->setSubsystem(subsystem);
+    // data and then populate the primary SRC and SRC words for the custom PEL
+    // based on the subsystem's TI info.
+    std::map<std::string, std::string>::iterator it;
+    uint8_t subsystem;
+
+    it = i_additional.find("Subsystem");
+    if (it != i_additional.end())
+    {
+        subsystem = std::stoi(it->second);
+        tiPel->setSubsystem(subsystem);
+    }
+    else
+    {
+        // The entry with key "Subsystem" does not exist in the additional map.
+        // Log the error, create failure event, and return.
+        trace::err("Error the key %s does not exist in the map.", "Subsystem");
+        eventAttentionFail((int)AttnSection::attnLogging | ATTN_INVALID_KEY);
+        return;
+    }
 
     // If recoverable attentions are active we will call the analyzer and
     // then link the custom pel to analyzer pel.
-    std::map<std::string, std::string>::iterator it;
     it = i_additional.find("recoverables");
     if (it != i_additional.end() && "true" == it->second)
     {
@@ -146,9 +160,24 @@
 
         // char array for raw pel src
         std::array<char, pel::asciiStringSize> srcChars{'0'};
+        std::string srcString;
 
         // src from TI info
-        std::string srcString = i_additional["SrcAscii"];
+        it = i_additional.find("SrcAscii");
+        if (it != i_additional.end())
+        {
+            srcString = it->second;
+        }
+        else
+        {
+            // The entry with key "Subsystem" does not exist in the additional
+            // map. Log the error, create failure event, and return.
+            trace::err("Error the key %s does not exist in the map.",
+                       "SrcAscii");
+            eventAttentionFail((int)AttnSection::attnLogging |
+                               ATTN_INVALID_KEY);
+            return;
+        }
 
         // copy from string to char array
         srcString.copy(srcChars.data(),
@@ -157,7 +186,7 @@
         tiPel->setAsciiString(srcChars); // pel object src is char array
 
         // set symptom-id
-        auto symptomId = (i_additional["SrcAscii"].substr(0, 8) + '_');
+        auto symptomId = (srcString.substr(0, 8) + '_');
 
         symptomId += (i_additional["0x10 SRC Word 12"]);
         symptomId += (i_additional["0x14 SRC Word 13"] + '_');
@@ -191,9 +220,24 @@
 
         // char array for raw pel src
         std::array<char, pel::asciiStringSize> srcChars{'0'};
+        std::string srcString;
 
         // src from TI info
-        std::string srcString = i_additional["SrcAscii"];
+        it = i_additional.find("SrcAscii");
+        if (it != i_additional.end())
+        {
+            srcString = it->second;
+        }
+        else
+        {
+            // The entry with key "Subsystem" does not exist in the additional
+            // map. Log the error, create failure event, and return.
+            trace::err("Error the key %s does not exist in the map.",
+                       "SrcAscii");
+            eventAttentionFail((int)AttnSection::attnLogging |
+                               ATTN_INVALID_KEY);
+            return;
+        }
 
         // copy from string to char array
         srcString.copy(srcChars.data(),
@@ -202,7 +246,7 @@
         tiPel->setAsciiString(srcChars); // pel object src is char array
 
         // set symptom-id
-        auto symptomId = (i_additional["SrcAscii"].substr(0, 8) + '_');
+        auto symptomId = (srcString.substr(0, 8) + '_');
 
         symptomId += (i_additional["0x10 HB Word 0"]);       // note: word 1
         symptomId += (i_additional["0x14 HB Word 2"] + '_'); // does not exist
@@ -334,7 +378,25 @@
                 close(pelFd);
             }
 
-            uint8_t subsystem = std::stoi(i_additional["Subsystem"]);
+            std::map<std::string, std::string>::iterator it;
+            uint8_t subsystem;
+
+            it = i_additional.find("Subsystem");
+            if (it != i_additional.end())
+            {
+                subsystem = std::stoi(it->second);
+            }
+            else
+            {
+                // The entry with key "Subsystem" does not exist in the
+                // additional map. Log the error, create failure event, and
+                // return.
+                trace::err("Error the key %s does not exist in the map.",
+                           "Subsystem");
+                eventAttentionFail((int)AttnSection::attnLogging |
+                                   ATTN_INVALID_KEY);
+                return 0;
+            }
 
             // If not hypervisor TI
             if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) != subsystem)
@@ -370,7 +432,24 @@
     {
         tiInfoSize = 56; // assume not hypervisor TI
 
-        uint8_t subsystem = std::stoi(i_additionalData["Subsystem"]);
+        std::map<std::string, std::string>::iterator it;
+        uint8_t subsystem;
+
+        it = i_additionalData.find("Subsystem");
+        if (it != i_additionalData.end())
+        {
+            subsystem = std::stoi(it->second);
+        }
+        else
+        {
+            // The entry with key "Subsystem" does not exist in the additional
+            // map. Log the error, create failure event, and return.
+            trace::err("Error the key %s does not exist in the map.",
+                       "Subsystem");
+            eventAttentionFail((int)AttnSection::attnLogging |
+                               ATTN_INVALID_KEY);
+            return;
+        }
 
         // If hypervisor
         if (static_cast<uint8_t>(pel::SubsystemID::hypervisor) == subsystem)