Skip to content

Commit 8e73826

Browse files
committed
[[ Bug 19866 ]] Emit (null) for %@ when parameter is nullptr
This patch changes MCStringFormat so that if %@ is encountered and the corresponding parameter is nullptr, it emits (null) rather than crashing.
1 parent 1c6d008 commit 8e73826

File tree

2 files changed

+13
-1
lines changed

2 files changed

+13
-1
lines changed

libfoundation/src/foundation-string.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1204,7 +1204,9 @@ bool MCStringFormatV(MCStringRef& r_string, const char *p_format, va_list p_args
12041204
t_value = va_arg(p_args, MCValueRef);
12051205

12061206
MCAutoStringRef t_string;
1207-
if (MCValueGetTypeCode(t_value) == kMCValueTypeCodeString)
1207+
if (t_value == nullptr)
1208+
t_string = MCSTR("(null)");
1209+
else if (MCValueGetTypeCode(t_value) == kMCValueTypeCodeString)
12081210
t_string = (MCStringRef)t_value;
12091211
else
12101212
/* UNCHECKED */ MCValueCopyDescription (t_value, &t_string);

libfoundation/test/test_string.cpp

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,16 @@ TEST(string, format_stringref_with_range)
123123
ASSERT_STREQ(MCStringGetCString(*t_string), "hello");
124124
}
125125

126+
TEST(string, format_null_valueref)
127+
//
128+
// Checks that a nullptr valueref maps to (null) [ Bug 19866 ]
129+
//
130+
{ MCAutoStringRef t_string;
131+
132+
ASSERT_TRUE(MCStringFormat(&t_string, "%@", nullptr));
133+
ASSERT_STREQ(MCStringGetCString(*t_string), "(null)");
134+
}
135+
126136
TEST(string, format_everything)
127137
//
128138
// Checks formatting with lots of different options

0 commit comments

Comments
 (0)