Skip to content

Commit 66800a7

Browse files
committed
Don't assume nul termination in printf
Thanks ZUENS2020 for reporting.
1 parent ea85b81 commit 66800a7

File tree

3 files changed

+13
-3
lines changed

3 files changed

+13
-3
lines changed

.github/workflows/linux.yml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,8 @@ jobs:
4848
- cxx: clang++-14
4949
build_type: Debug
5050
std: 20
51+
cxxflags: -fsanitize=address,undefined
52+
cxxflags_extra: -fno-sanitize-recover=all -fno-omit-frame-pointer
5153
- cxx: clang++-14
5254
build_type: Debug
5355
std: 20
@@ -154,7 +156,7 @@ jobs:
154156
working-directory: ${{runner.workspace}}/build
155157
env:
156158
CXX: ${{matrix.cxx}}
157-
CXXFLAGS: ${{matrix.cxxflags}}
159+
CXXFLAGS: ${{matrix.cxxflags}} ${{matrix.cxxflags_extra}}
158160
run: |
159161
cmake -DCMAKE_BUILD_TYPE=${{matrix.build_type}} \
160162
-DCMAKE_CXX_STANDARD=${{matrix.std}} \

include/fmt/printf.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,6 +330,7 @@ template <typename Char, typename GetArg>
330330
auto parse_header(const Char*& it, const Char* end, format_specs& specs,
331331
GetArg get_arg) -> int {
332332
int arg_index = -1;
333+
if (it == end) return arg_index;
333334
Char c = *it;
334335
if (c >= '0' && c <= '9') {
335336
// Parse an argument index (if followed by '$') or a width possibly

test/printf-test.cc

Lines changed: 9 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,8 @@ 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(fmt::sprintf(fmt::string_view(p.get(), 1)), format_error);
564+
}

0 commit comments

Comments
 (0)