psu-ng: Use regex to replace invalid VPD data

Passing non-ASCII/unprintable VPD character data to D-Bus or other
applications can lead to crashes or other unexpected behavior.

Change-Id: I9a70f4505d330652e36143395ff92902a203fa44
Signed-off-by: Brandon Wyman <bjwyman@gmail.com>
diff --git a/phosphor-power-supply/power_supply.cpp b/phosphor-power-supply/power_supply.cpp
index e9b4c83..35b23b2 100644
--- a/phosphor-power-supply/power_supply.cpp
+++ b/phosphor-power-supply/power_supply.cpp
@@ -13,6 +13,7 @@
 #include <cmath>
 #include <cstdint> // uint8_t...
 #include <fstream>
+#include <regex>
 #include <thread> // sleep_for()
 
 namespace phosphor::power::psu
@@ -808,6 +809,8 @@
                                const std::size_t& vpdSize)
 {
     std::string vpdValue;
+    const std::regex illegalVPDRegex =
+        std::regex("[^[:alnum:]]", std::regex::basic);
 
     try
     {
@@ -831,6 +834,10 @@
         vpdValue.resize(vpdSize, ' ');
     }
 
+    // Replace any illegal values with space(s).
+    std::regex_replace(vpdValue.begin(), vpdValue.begin(), vpdValue.end(),
+                       illegalVPDRegex, " ");
+
     return vpdValue;
 }
 
diff --git a/phosphor-power-supply/power_supply.hpp b/phosphor-power-supply/power_supply.hpp
index d60b3f0..9cdc574 100644
--- a/phosphor-power-supply/power_supply.hpp
+++ b/phosphor-power-supply/power_supply.hpp
@@ -873,7 +873,11 @@
      */
     auto getMaxPowerOut() const;
 
-    /* @brief Reads a VPD value from PMBus and corrects for size.
+    /* @brief Reads a VPD value from PMBus, correct size, and contents.
+     *
+     * If the VPD data read is not the passed in size, resize and fill with
+     * spaces. If the data contains a non-alphanumeric value, replace any of
+     * those values with spaces.
      *
      * @param[in] vpdName - The name of the sysfs "file" to read data from.
      * @param[in] type - The HWMON file type to read from.