]>
git.proxmox.com Git - ceph.git/blob - ceph/src/fmt/test/fuzzing/sprintf.cpp
1 // Copyright (c) 2019, Paul Dreik
2 // License: see LICENSE.rst in the fmt root directory
3 #include <fmt/format.h>
4 #include <fmt/printf.h>
8 #include "fuzzer_common.h"
10 using fmt_fuzzer::Nfixed
;
12 template <typename Item1
, typename Item2
>
13 void invoke_fmt(const uint8_t* Data
, size_t Size
) {
14 constexpr auto N1
= sizeof(Item1
);
15 constexpr auto N2
= sizeof(Item2
);
16 static_assert(N1
<= Nfixed
, "size1 exceeded");
17 static_assert(N2
<= Nfixed
, "size2 exceeded");
18 if (Size
<= Nfixed
+ Nfixed
) {
21 Item1 item1
= fmt_fuzzer::assignFromBuf
<Item1
>(Data
);
25 Item2 item2
= fmt_fuzzer::assignFromBuf
<Item2
>(Data
);
29 auto fmtstring
= fmt::string_view(fmt_fuzzer::as_chars(Data
), Size
);
31 #if FMT_FUZZ_FORMAT_TO_STRING
32 std::string message
= fmt::format(fmtstring
, item1
, item2
);
34 fmt::memory_buffer message
;
35 fmt::format_to(message
, fmtstring
, item1
, item2
);
39 // for dynamic dispatching to an explicit instantiation
40 template <typename Callback
> void invoke(int index
, Callback callback
) {
49 using sc
= signed char;
53 using uc
= unsigned char;
60 using us
= unsigned short;
73 using ul
= unsigned long;
83 using LD
= long double;
93 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data
, size_t Size
) {
98 // switch types depending on the first byte of the input
99 const auto first
= Data
[0] & 0x0F;
100 const auto second
= (Data
[0] & 0xF0) >> 4;
104 auto outer
= [=](auto param1
) {
105 auto inner
= [=](auto param2
) {
106 invoke_fmt
<decltype(param1
), decltype(param2
)>(Data
, Size
);
108 invoke(second
, inner
);
112 invoke(first
, outer
);
113 } catch (std::exception
& /*e*/) {