Skip to content

Commit 6509836

Browse files
committed
Fix fmt function to handle null pointers and return empty string
1 parent d7c270e commit 6509836

File tree

2 files changed

+7
-5
lines changed

2 files changed

+7
-5
lines changed

src/libipc/imp/fmt.cpp

+5-2
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,11 @@ bool to_string(fmt_context &ctx, std::string const &a) noexcept {
221221
}
222222

223223
bool to_string(fmt_context &ctx, char const *a, span<char const> fstr) noexcept {
224-
if (a == nullptr) return false;
225-
return ipc::sprintf(ctx, fmt_of, fstr, "s", a);
224+
if (a == nullptr) {
225+
return ipc::sprintf(ctx, fmt_of, fstr, "s", "");
226+
} else {
227+
return ipc::sprintf(ctx, fmt_of, fstr, "s", a);
228+
}
226229
}
227230

228231
bool to_string(fmt_context &ctx, char a) noexcept {

test/imp/test_imp_fmt.cpp

+2-3
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,10 @@ TEST(fmt, fmt) {
112112
EXPECT_EQ(s, test);
113113

114114
EXPECT_EQ(ipc::fmt("", 1, "", '2', "", 3.0), "123.000000");
115+
char const * nc = nullptr;
116+
EXPECT_EQ(ipc::fmt(nc, 1, "", '2', "", 3.0), "123.000000");
115117
std::string empty;
116118
EXPECT_EQ(ipc::fmt(empty, 1, "", '2', "", 3.0), "123.000000");
117-
EXPECT_EQ(ipc::fmt(empty, 1, empty, '2', "", 3.0), "123.000000");
118-
EXPECT_EQ(ipc::fmt("", 1, empty, '2', empty, 3.0), "123.000000");
119-
EXPECT_EQ(ipc::fmt("", 1, "", '2', empty), "12");
120119
}
121120

122121
namespace {

0 commit comments

Comments
 (0)