Skip to content

Commit 32f63fc

Browse files
rustyconoverTrott
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 <[email protected]> Reviewed-By: Denys Otrishko <[email protected]> Reviewed-By: Richard Lau <[email protected]> Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Rich Trott <[email protected]> Reviewed-By: James M Snell <[email protected]>
1 parent 2462a2c commit 32f63fc

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)