Skip to content

Commit 91dd101

Browse files
rustyconovercodebytere
authored andcommitted
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: #31523 Reviewed-By: Colin Ihrig <cjihrig@gmail.com> Reviewed-By: Denys Otrishko <shishugi@gmail.com> Reviewed-By: Richard Lau <riclau@uk.ibm.com> Reviewed-By: Anna Henningsen <anna@addaleax.net> Reviewed-By: Rich Trott <rtrott@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent d978bb5 commit 91dd101

File tree

2 files changed

+4
-1
lines changed

2 files changed

+4
-1
lines changed

src/debug_utils-inl.h

+3-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ struct ToStringHelper {
2121
enable_if<std::is_arithmetic<T>::value, bool>::type,
2222
typename dummy = bool>
2323
static std::string Convert(const T& value) { return std::to_string(value); }
24-
static std::string Convert(const char* value) { return value; }
24+
static std::string Convert(const char* value) {
25+
return value != nullptr ? value : "(null)";
26+
}
2527
static std::string Convert(const std::string& value) { return value; }
2628
static std::string Convert(bool value) { return value ? "true" : "false"; }
2729
};

test/cctest/test_util.cc

+1
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@ TEST(UtilTest, SPrintF) {
281281
const char* bar = "bar";
282282
EXPECT_EQ(SPrintF("%s %s", foo, "bar"), "foo bar");
283283
EXPECT_EQ(SPrintF("%s %s", foo, bar), "foo bar");
284+
EXPECT_EQ(SPrintF("%s", nullptr), "(null)");
284285

285286
EXPECT_EQ(SPrintF("[%% %s %%]", foo), "[% foo %]");
286287

0 commit comments

Comments
 (0)