Files
fmt/test/fuzzing/named-arg.cc
T

103 lines
2.2 KiB
C++
Raw Normal View History

2019-06-30 09:11:13 +02:00
// Copyright (c) 2019, Paul Dreik
// For the license information refer to format.h.
2019-06-30 09:11:13 +02:00
2021-08-22 20:18:11 +02:00
#include <fmt/chrono.h>
2019-06-30 09:11:13 +02:00
#include <cstdint>
#include <type_traits>
#include <vector>
2020-10-11 08:30:14 -07:00
#include "fuzzer-common.h"
2019-06-30 09:11:13 +02:00
2020-10-13 08:08:48 -07:00
template <typename T>
void invoke_fmt(const uint8_t* data, size_t size, unsigned arg_name_size) {
static_assert(sizeof(T) <= fixed_size, "fixed_size too small");
if (size <= fixed_size) return;
const T value = assign_from_buf<T>(data);
data += fixed_size;
size -= fixed_size;
2019-06-30 09:11:13 +02:00
2020-10-13 08:08:48 -07:00
if (arg_name_size <= 0 || arg_name_size >= size) return;
data_to_string arg_name(data, arg_name_size, true);
data += arg_name_size;
size -= arg_name_size;
2019-06-30 09:11:13 +02:00
2020-10-13 08:08:48 -07:00
data_to_string format_str(data, size);
2020-10-14 07:13:37 -07:00
try {
2019-06-30 09:11:13 +02:00
#if FMT_FUZZ_FORMAT_TO_STRING
2020-10-14 07:13:37 -07:00
std::string message =
2021-08-22 20:18:11 +02:00
fmt::format(format_str.get(), fmt::arg(arg_name.data(), value));
2019-06-30 09:11:13 +02:00
#else
2020-10-14 07:13:37 -07:00
fmt::memory_buffer out;
2021-09-06 10:24:21 -07:00
fmt::format_to(std::back_inserter(out), format_str.get(),
fmt::arg(arg_name.data(), value));
2019-06-30 09:11:13 +02:00
#endif
2020-10-14 07:13:37 -07:00
} catch (std::exception&) {
}
2019-06-30 09:11:13 +02:00
}
// For dynamic dispatching to an explicit instantiation.
2020-10-13 08:08:48 -07:00
template <typename Callback> void invoke(int type, Callback callback) {
switch (type) {
2019-06-30 09:11:13 +02:00
case 0:
callback(bool());
2019-06-30 09:11:13 +02:00
break;
case 1:
callback(char());
2019-06-30 09:11:13 +02:00
break;
case 2:
using sc = signed char;
callback(sc());
2019-06-30 09:11:13 +02:00
break;
case 3:
using uc = unsigned char;
callback(uc());
2019-06-30 09:11:13 +02:00
break;
case 4:
callback(short());
2019-06-30 09:11:13 +02:00
break;
case 5:
using us = unsigned short;
callback(us());
2019-06-30 09:11:13 +02:00
break;
case 6:
callback(int());
2019-06-30 09:11:13 +02:00
break;
case 7:
callback(unsigned());
2019-06-30 09:11:13 +02:00
break;
case 8:
callback(long());
2019-06-30 09:11:13 +02:00
break;
case 9:
using ul = unsigned long;
callback(ul());
2019-06-30 09:11:13 +02:00
break;
case 10:
callback(float());
2019-06-30 09:11:13 +02:00
break;
case 11:
callback(double());
2019-06-30 09:11:13 +02:00
break;
case 12:
using LD = long double;
callback(LD());
2019-06-30 09:11:13 +02:00
break;
}
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (size <= 3) return 0;
2019-06-30 09:11:13 +02:00
// Switch types depending on the first byte of the input.
2020-10-13 08:08:48 -07:00
const auto type = data[0] & 0x0F;
const unsigned arg_name_size = (data[0] & 0xF0) >> 4;
data++;
size--;
2019-06-30 09:11:13 +02:00
2020-10-14 07:13:37 -07:00
invoke(type, [=](auto arg) {
invoke_fmt<decltype(arg)>(data, size, arg_name_size);
});
2019-06-30 09:11:13 +02:00
return 0;
}