From 218aead649d970177df2a6486a33691400c9c8c0 Mon Sep 17 00:00:00 2001 From: Rusty Conover Date: Sun, 26 Jan 2020 10:53:26 -0500 Subject: [PATCH 1/2] debug: Fix crash handling null strings When internal debug is enabled output null strings as "(null)" rather than crashing matching glibc's behavior. --- src/debug_utils-inl.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/debug_utils-inl.h b/src/debug_utils-inl.h index f24643fbae3ad8..f84d51d8112090 100644 --- a/src/debug_utils-inl.h +++ b/src/debug_utils-inl.h @@ -21,7 +21,9 @@ struct ToStringHelper { enable_if::value, bool>::type, typename dummy = bool> static std::string Convert(const T& value) { return std::to_string(value); } - static std::string Convert(const char* value) { return value; } + static std::string Convert(const char* value) { + return value ? value : "(null)"; + } static std::string Convert(const std::string& value) { return value; } static std::string Convert(bool value) { return value ? "true" : "false"; } }; From 6727bc0fcab49e226e074de391d56f23646915c6 Mon Sep 17 00:00:00 2001 From: Rusty Conover Date: Sun, 26 Jan 2020 11:43:28 -0500 Subject: [PATCH 2/2] Add test and fix nit. --- src/debug_utils-inl.h | 2 +- test/cctest/test_util.cc | 1 + 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/src/debug_utils-inl.h b/src/debug_utils-inl.h index f84d51d8112090..2f6137700d8a37 100644 --- a/src/debug_utils-inl.h +++ b/src/debug_utils-inl.h @@ -22,7 +22,7 @@ struct ToStringHelper { typename dummy = bool> static std::string Convert(const T& value) { return std::to_string(value); } static std::string Convert(const char* value) { - return value ? value : "(null)"; + return value != nullptr ? value : "(null)"; } static std::string Convert(const std::string& value) { return value; } static std::string Convert(bool value) { return value ? "true" : "false"; } diff --git a/test/cctest/test_util.cc b/test/cctest/test_util.cc index 843d16d9f527c6..a38f549f0387dc 100644 --- a/test/cctest/test_util.cc +++ b/test/cctest/test_util.cc @@ -281,6 +281,7 @@ TEST(UtilTest, SPrintF) { const char* bar = "bar"; EXPECT_EQ(SPrintF("%s %s", foo, "bar"), "foo bar"); EXPECT_EQ(SPrintF("%s %s", foo, bar), "foo bar"); + EXPECT_EQ(SPrintF("%s", nullptr), "(null)"); EXPECT_EQ(SPrintF("[%% %s %%]", foo), "[% foo %]");