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