Fix coding style and remove duplicate fuzzer

This commit is contained in:
Victor Zverovich
2020-10-11 07:45:39 -07:00
parent 41d97e1ef4
commit 7e56b6b6cb
9 changed files with 245 additions and 411 deletions

View File

@@ -1,112 +1,108 @@
// Copyright (c) 2019, Paul Dreik
// License: see LICENSE.rst in the fmt root directory
#include <fmt/format.h>
// For the license information refer to format.h.
#include <cstdint>
#include <stdexcept>
#include <type_traits>
#include <exception>
#include <string>
#include <fmt/format.h>
#include "fuzzer_common.h"
constexpr auto Nfixed = fmt_fuzzer::Nfixed;
constexpr auto nfixed = fmt_fuzzer::nfixed;
template <typename Item1, typename Item2>
void invoke_fmt(const uint8_t* Data, size_t Size) {
constexpr auto N1 = sizeof(Item1);
constexpr auto N2 = sizeof(Item2);
static_assert(N1 <= Nfixed, "size1 exceeded");
static_assert(N2 <= Nfixed, "size2 exceeded");
if (Size <= Nfixed + Nfixed) {
return;
}
const Item1 item1 = fmt_fuzzer::assignFromBuf<Item1>(Data);
Data += Nfixed;
Size -= Nfixed;
void invoke_fmt(const uint8_t* data, size_t size) {
static_assert(sizeof(Item1) <= nfixed, "size1 exceeded");
static_assert(sizeof(Item2) <= nfixed, "size2 exceeded");
if (size <= nfixed + nfixed) return;
const Item2 item2 = fmt_fuzzer::assignFromBuf<Item2>(Data);
Data += Nfixed;
Size -= Nfixed;
const Item1 item1 = fmt_fuzzer::assignFromBuf<Item1>(data);
data += nfixed;
size -= nfixed;
auto fmtstring = fmt::string_view(fmt_fuzzer::as_chars(Data), Size);
const Item2 item2 = fmt_fuzzer::assignFromBuf<Item2>(data);
data += nfixed;
size -= nfixed;
auto format_str = fmt::string_view(fmt_fuzzer::as_chars(data), size);
#if FMT_FUZZ_FORMAT_TO_STRING
std::string message = fmt::format(fmtstring, item1, item2);
std::string message = fmt::format(format_str, item1, item2);
#else
fmt::memory_buffer message;
fmt::format_to(message, fmtstring, item1, item2);
fmt::format_to(message, format_str, item1, item2);
#endif
}
// for dynamic dispatching to an explicit instantiation
// For dynamic dispatching to an explicit instantiation.
template <typename Callback> void invoke(int index, Callback callback) {
switch (index) {
case 0:
callback(bool{});
callback(bool());
break;
case 1:
callback(char{});
callback(char());
break;
case 2:
using sc = signed char;
callback(sc{});
callback(sc());
break;
case 3:
using uc = unsigned char;
callback(uc{});
callback(uc());
break;
case 4:
callback(short{});
callback(short());
break;
case 5:
using us = unsigned short;
callback(us{});
callback(us());
break;
case 6:
callback(int{});
callback(int());
break;
case 7:
callback(unsigned{});
callback(unsigned());
break;
case 8:
callback(long{});
callback(long());
break;
case 9:
using ul = unsigned long;
callback(ul{});
callback(ul());
break;
case 10:
callback(float{});
callback(float());
break;
case 11:
callback(double{});
callback(double());
break;
case 12:
using LD = long double;
callback(LD{});
callback(LD());
break;
case 13:
using ptr = void*;
callback(ptr());
break;
}
}
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, size_t Size) {
if (Size <= 3) {
return 0;
}
// switch types depending on the first byte of the input
const auto first = Data[0] & 0x0F;
const auto second = (Data[0] & 0xF0) >> 4;
Data++;
Size--;
auto outer = [=](auto param1) {
auto inner = [=](auto param2) {
invoke_fmt<decltype(param1), decltype(param2)>(Data, Size);
};
invoke(second, inner);
};
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
if (size <= 3) return 0;
// Switch types depending on the first byte of the input.
const auto first = data[0] & 0x0F;
const auto second = (data[0] & 0xF0) >> 4;
data++;
size--;
try {
invoke(first, outer);
} catch (std::exception& /*e*/) {
invoke(first, [=](auto param1) {
invoke(second, [=](auto param2) {
invoke_fmt<decltype(param1), decltype(param2)>(data, size);
});
});
} catch (std::exception&) {
}
return 0;
}