peltool: Fixing memory leak in peltool

On analysis of a defect which points BMC reboot issue, it is found that
peltool has a memory leak which increases the memory consumption on
runtime.

Done the analysis for the same by generating valgrind report for peltool
and figured out that a block of memory which is getting allocated
dynamically in the API dumpHex() is not getting freed properly, results
in memory leak.

Fixing the same by changing the pointer which points to the block of
memory to a unique_ptr.

Tested:
The new valgrind report generated for peltool after adding the changes
shows zero leakage.
```
==883== LEAK SUMMARY:
==883==    definitely lost: 0 bytes in 0 blocks
==883==    indirectly lost: 0 bytes in 0 blocks
==883==      possibly lost: 0 bytes in 0 blocks
==883==    still reachable: 9,786 bytes in 31 blocks
==883==         suppressed: 0 bytes in 0 blocks
```

Signed-off-by: Arya K Padman <aryakpadman@gmail.com>
Change-Id: I55db4feedff3a139d0f44a5ace1778acc7375596
diff --git a/extensions/openpower-pels/json_utils.cpp b/extensions/openpower-pels/json_utils.cpp
index 13094ed..fa3246e 100644
--- a/extensions/openpower-pels/json_utils.cpp
+++ b/extensions/openpower-pels/json_utils.cpp
@@ -73,7 +73,8 @@
 
     return output;
 }
-char* dumpHex(const void* data, size_t size, size_t indentCount, bool toJson)
+std::unique_ptr<char[]> dumpHex(const void* data, size_t size,
+                                size_t indentCount, bool toJson)
 {
     const int symbolSize = 100;
     std::string jsonIndent(indentLevel * indentCount, 0x20);
@@ -81,7 +82,7 @@
     {
         jsonIndent.append("\"");
     }
-    char* buffer = (char*)calloc(std::max(70, 10 * (int)size), sizeof(char));
+    std::unique_ptr<char[]> buffer{new char[std::max(70, 10 * (int)size)]()};
     char* symbol = (char*)calloc(symbolSize, sizeof(char));
     char* byteCount = (char*)calloc(11, sizeof(char));
     char ascii[17];
@@ -94,12 +95,12 @@
             if (!toJson)
             {
                 snprintf(byteCount, 11, "%08X  ", static_cast<uint32_t>(i));
-                strcat(buffer, byteCount);
+                strcat(buffer.get(), byteCount);
             }
-            strcat(buffer, jsonIndent.c_str());
+            strcat(buffer.get(), jsonIndent.c_str());
         }
         snprintf(symbol, symbolSize, "%02X ", ((unsigned char*)data)[i]);
-        strcat(buffer, symbol);
+        strcat(buffer.get(), symbol);
         memset(symbol, 0, strlen(symbol));
         if (((unsigned char*)data)[i] >= ' ' &&
             ((unsigned char*)data)[i] <= '~')
@@ -117,7 +118,7 @@
             {
                 asciiString = escapeJSON(asciiString);
             }
-            strcat(buffer, " ");
+            strcat(buffer.get(), " ");
             if ((i + 1) % 16 == 0)
             {
                 if (i + 1 != size && toJson)
@@ -135,7 +136,7 @@
                     snprintf(symbol, symbolSize, "|  %s\n",
                              asciiString.c_str());
                 }
-                strcat(buffer, symbol);
+                strcat(buffer.get(), symbol);
                 memset(symbol, 0, strlen(symbol));
             }
             else if (i + 1 == size)
@@ -143,11 +144,11 @@
                 ascii[(i + 1) % 16] = '\0';
                 if ((i + 1) % 16 <= 8)
                 {
-                    strcat(buffer, " ");
+                    strcat(buffer.get(), " ");
                 }
                 for (j = (i + 1) % 16; j < 16; ++j)
                 {
-                    strcat(buffer, "   ");
+                    strcat(buffer.get(), "   ");
                 }
                 std::string asciiString2(ascii);
                 if (toJson)
@@ -162,7 +163,7 @@
                              asciiString2.c_str());
                 }
 
-                strcat(buffer, symbol);
+                strcat(buffer.get(), symbol);
                 memset(symbol, 0, strlen(symbol));
             }
         }
diff --git a/extensions/openpower-pels/json_utils.hpp b/extensions/openpower-pels/json_utils.hpp
index c972459..bf90d9b 100644
--- a/extensions/openpower-pels/json_utils.hpp
+++ b/extensions/openpower-pels/json_utils.hpp
@@ -8,6 +8,7 @@
 #include <fstream>
 #include <iomanip>
 #include <iostream>
+#include <memory>
 #include <string>
 #include <vector>
 
@@ -31,10 +32,10 @@
  * @param[in] size_t indentCount - The number of indent levels to indent
  * @param[in] bool toJson - if true, output lines as JSON array, else print
  *            output as plain text
- * @return char * - the Hex dump
+ * @return std::unique_ptr<char[]> - the Hex dump
  */
-char* dumpHex(const void* data, size_t size, size_t indentCount,
-              bool toJson = true);
+std::unique_ptr<char[]> dumpHex(const void* data, size_t size,
+                                size_t indentCount, bool toJson = true);
 
 /**
  * @brief Inserts key-value into a JSON string
diff --git a/extensions/openpower-pels/pel.cpp b/extensions/openpower-pels/pel.cpp
index 1f0ab67..64244c3 100644
--- a/extensions/openpower-pels/pel.cpp
+++ b/extensions/openpower-pels/pel.cpp
@@ -395,7 +395,8 @@
             section.flatten(s);
             std::string dstr =
                 dumpHex(std::data(data) + SectionHeader::flattenedSize(),
-                        data.size() - SectionHeader::flattenedSize(), 2);
+                        data.size() - SectionHeader::flattenedSize(), 2)
+                    .get();
             std::string jsonIndent(indentLevel, 0x20);
             buf += jsonIndent + "\"Data\": [\n";
             buf += dstr;
diff --git a/extensions/openpower-pels/tools/peltool.cpp b/extensions/openpower-pels/tools/peltool.cpp
index 063127f..a4b7f59 100644
--- a/extensions/openpower-pels/tools/peltool.cpp
+++ b/extensions/openpower-pels/tools/peltool.cpp
@@ -359,8 +359,9 @@
         }
         if (hexDump)
         {
-            std::cout << dumpHex(std::data(pel.data()), pel.size(), 0, false)
-                      << std::endl;
+            std::cout
+                << dumpHex(std::data(pel.data()), pel.size(), 0, false).get()
+                << std::endl;
         }
         else if (fullPEL)
         {
@@ -681,8 +682,8 @@
     {
         if (hexDump)
         {
-            std::string dstr = dumpHex(std::data(pel.data()), pel.size(), 0,
-                                       false);
+            std::string dstr =
+                dumpHex(std::data(pel.data()), pel.size(), 0, false).get();
             std::cout << dstr << std::endl;
         }
         else
@@ -888,8 +889,8 @@
             PEL pel{data};
             if (hexDump)
             {
-                std::string dstr = dumpHex(std::data(pel.data()), pel.size(), 0,
-                                           false);
+                std::string dstr =
+                    dumpHex(std::data(pel.data()), pel.size(), 0, false).get();
                 std::cout << dstr << std::endl;
             }
             else