Skip to content

Commit 5546554

Browse files
committed
Test lack of nul termination in printf
1 parent ea85b81 commit 5546554

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

test/printf-test.cc

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -313,7 +313,8 @@ TEST(printf_test, positional_precision) {
313313
EXPECT_EQ("Hell", test_sprintf("%2$.*1$s", 4, "Hello"));
314314
EXPECT_THROW_MSG(test_sprintf("%2$.*1$d", 5.0, 42), format_error,
315315
"precision is not integer");
316-
EXPECT_THROW_MSG(test_sprintf("%2$.*1$d"), format_error, "argument not found");
316+
EXPECT_THROW_MSG(test_sprintf("%2$.*1$d"), format_error,
317+
"argument not found");
317318
EXPECT_THROW_MSG(test_sprintf("%2$.*1$d", big_num, 42), format_error,
318319
"number is too big");
319320
}
@@ -322,7 +323,8 @@ TEST(printf_test, positional_width_and_precision) {
322323
EXPECT_EQ(" 00042", test_sprintf("%3$*1$.*2$d", 7, 5, 42));
323324
EXPECT_EQ(" ab", test_sprintf("%3$*1$.*2$s", 7, 2, "abcdef"));
324325
EXPECT_EQ(" 00042", test_sprintf("%3$*1$.*2$x", 7, 5, 0x42));
325-
EXPECT_EQ("100.4400000", test_sprintf("%6$-*5$.*4$f%3$s%2$s%1$s", "", "", "", 7, 4, 100.44));
326+
EXPECT_EQ("100.4400000",
327+
test_sprintf("%6$-*5$.*4$f%3$s%2$s%1$s", "", "", "", 7, 4, 100.44));
326328
}
327329

328330
template <typename T> struct make_signed {
@@ -555,3 +557,9 @@ TEST(printf_test, make_printf_args) {
555557
fmt::vsprintf(fmt::basic_string_view<wchar_t>(L"[%d] %s happened"),
556558
{fmt::make_printf_args<wchar_t>(n, L"something")}));
557559
}
560+
561+
TEST(printf_test, trailing_percent_non_nul_terminated) {
562+
auto p = std::unique_ptr<char>(new char('%'));
563+
EXPECT_THROW_MSG(fmt::sprintf(fmt::string_view(p.get(), 1)), format_error,
564+
"invalid format string");
565+
}

0 commit comments

Comments
 (0)