Skip to content

Commit

Permalink
src: fix debug crash handling null strings
Browse files Browse the repository at this point in the history
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]>
  • Loading branch information
rustyconover authored and codebytere committed Mar 15, 2020
1 parent 8ecac02 commit c8d71ca
Show file tree
Hide file tree
Showing 2 changed files with 4 additions and 1 deletion.
4 changes: 3 additions & 1 deletion src/debug_utils-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,9 @@ struct ToStringHelper {
enable_if<std::is_arithmetic<T>::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"; }
};
Expand Down
1 change: 1 addition & 0 deletions test/cctest/test_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -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 %]");

Expand Down

0 comments on commit c8d71ca

Please sign in to comment.