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));
             }
         }