From f04576ede0b79e2f73c16b105da384fd9a2d47e0 Mon Sep 17 00:00:00 2001 From: Rusty Conover Date: Sun, 26 Jan 2020 10:53:26 -0500 Subject: [PATCH] src: fix debug crash handling null strings When internal debug is enabled, output null strings as "(null)" rather than crashing, matching glibc's behavior. PR-URL: https://github.com/nodejs/node/pull/31523 Reviewed-By: Colin Ihrig Reviewed-By: Denys Otrishko Reviewed-By: Richard Lau Reviewed-By: Anna Henningsen Reviewed-By: Rich Trott Reviewed-By: James M Snell --- src/debug_utils-inl.h | 4 +++- test/cctest/test_util.cc | 1 + 2 files changed, 4 insertions(+), 1 deletion(-) diff --git a/src/debug_utils-inl.h b/src/debug_utils-inl.h index f24643fbae3ad8..2f6137700d8a37 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 != 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 %]");