openpower-pels: json_utils: fix crash on invalid format string
The `getNumberString` function previously crashed if an invalid
format string was passed, because it would attempt to construct
a std::string from a nullptr. There was even a test case that
confirmed that the code crashed via a SIGSEGV!
AddressSanitizer, which is ran on all of our meson-based CIs,
does not allow this kind of behavior. Change `getNumberString`
so it throws a `std::invalid_argument` instead of crashing, which
itself might result in a `std::terminate` if the exception is
uncaught, but is at least acceptable behavior in the eyes of the
AddressSanitizer (and mine as well).
Signed-off-by: Patrick Williams <patrick@stwcx.xyz>
Change-Id: I06d25edc2ddf552169383e03595426bb065425be
diff --git a/test/openpower-pels/json_utils_test.cpp b/test/openpower-pels/json_utils_test.cpp
index 60ca00d..a7203aa 100644
--- a/test/openpower-pels/json_utils_test.cpp
+++ b/test/openpower-pels/json_utils_test.cpp
@@ -35,8 +35,7 @@
EXPECT_EQ(getNumberString("%d", number), "123");
EXPECT_EQ(getNumberString("%03X", number), "07B");
EXPECT_EQ(getNumberString("0x%X", number), "0x7B");
- ASSERT_EXIT((getNumberString("%123", number), exit(0)),
- ::testing::KilledBySignal(SIGSEGV), ".*");
+ EXPECT_THROW(getNumberString("%123", number), std::invalid_argument);
}
TEST(JsonUtilsTest, JsonInsertTest)