]>
git.proxmox.com Git - ceph.git/blob - ceph/src/fmt/test/fuzzing/named_arg.cpp
1 // Copyright (c) 2019, Paul Dreik
2 // License: see LICENSE.rst in the fmt root directory
4 #include <fmt/chrono.h>
10 #include "fuzzer_common.h"
12 template <typename Item1
>
13 void invoke_fmt(const uint8_t* Data
, size_t Size
, unsigned int argsize
) {
14 constexpr auto N1
= sizeof(Item1
);
15 static_assert(N1
<= fmt_fuzzer::Nfixed
, "Nfixed too small");
16 if (Size
<= fmt_fuzzer::Nfixed
) {
19 const Item1 item1
= fmt_fuzzer::assignFromBuf
<Item1
>(Data
);
21 Data
+= fmt_fuzzer::Nfixed
;
22 Size
-= fmt_fuzzer::Nfixed
;
24 // how many chars should be used for the argument name?
25 if (argsize
<= 0 || argsize
>= Size
) {
29 // allocating buffers separately is slower, but increases chances
30 // of detecting memory errors
31 #if FMT_FUZZ_SEPARATE_ALLOCATION
32 std::vector
<char> argnamebuffer(argsize
+ 1);
33 std::memcpy(argnamebuffer
.data(), Data
, argsize
);
34 auto argname
= argnamebuffer
.data();
36 auto argname
= fmt_fuzzer::as_chars(Data
);
41 #if FMT_FUZZ_SEPARATE_ALLOCATION
42 // allocates as tight as possible, making it easier to catch buffer overruns.
43 std::vector
<char> fmtstringbuffer(Size
);
44 std::memcpy(fmtstringbuffer
.data(), Data
, Size
);
45 auto fmtstring
= fmt::string_view(fmtstringbuffer
.data(), Size
);
47 auto fmtstring
= fmt::string_view(fmt_fuzzer::as_chars(Data
), Size
);
50 #if FMT_FUZZ_FORMAT_TO_STRING
51 std::string message
= fmt::format(fmtstring
, fmt::arg(argname
, item1
));
53 fmt::memory_buffer outbuf
;
54 fmt::format_to(outbuf
, fmtstring
, fmt::arg(argname
, item1
));
58 // for dynamic dispatching to an explicit instantiation
59 template <typename Callback
> void invoke(int index
, Callback callback
) {
68 using sc
= signed char;
72 using uc
= unsigned char;
79 using us
= unsigned short;
92 using ul
= unsigned long;
102 using LD
= long double;
108 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data
, size_t Size
) {
113 // switch types depending on the first byte of the input
114 const auto first
= Data
[0] & 0x0F;
115 const unsigned int second
= (Data
[0] & 0xF0) >> 4;
119 auto outerfcn
= [=](auto param1
) {
120 invoke_fmt
<decltype(param1
)>(Data
, Size
, second
);
124 invoke(first
, outerfcn
);
125 } catch (std::exception
& /*e*/) {