Skip to content

Commit

Permalink
clang format
Browse files Browse the repository at this point in the history
pauldreik committed Jun 9, 2019

Verified

This commit was signed with the committer’s verified signature. The key has expired.
vespian Paweł Rozlach
1 parent e0e361b commit a667365
Showing 2 changed files with 62 additions and 61 deletions.
91 changes: 46 additions & 45 deletions test/fuzzing/chrono_duration.cpp
Original file line number Diff line number Diff line change
@@ -1,23 +1,23 @@
// Copyright (c) 2019, Paul Dreik
// License: see LICENSE.rst in the fmt root directory

#include <fmt/chrono.h>
#include <fmt/core.h>
#include <cstdint>
#include <limits>
#include <stdexcept>
#include <type_traits>
#include <vector>
#include <fmt/chrono.h>

template <typename Item, typename Ratio>
void invoke_inner(fmt::string_view formatstring, const Item item) {
const std::chrono::duration<Item, Ratio> value(item);
try {
// Don't switch these two dynamically,
// there is already a large combinatoric explosion
// of type and ratio, causing afl to suffer and the corpus
// getting enormous. Occasionally, flip this switch and
// try manually.
// Don't switch these two dynamically,
// there is already a large combinatoric explosion
// of type and ratio, causing afl to suffer and the corpus
// getting enormous. Occasionally, flip this switch and
// try manually.
#if 0
std::string message = fmt::format(formatstring, value);
#else
@@ -31,74 +31,75 @@ void invoke_inner(fmt::string_view formatstring, const Item item) {
// Item is the underlying type for duration (int, long etc)
template <typename Item>
void invoke_outer(const uint8_t* Data, std::size_t Size, const int scaling) {
//always use a fixed location of the data, so different cases will
//cooperate better. the same bit pattern, interpreted as another type,
//is likely interesting.
constexpr auto Nfixed=std::max(sizeof(long double),sizeof(std::intmax_t));
// always use a fixed location of the data, so different cases will
// cooperate better. the same bit pattern, interpreted as another type,
// is likely interesting.
constexpr auto Nfixed = std::max(sizeof(long double), sizeof(std::intmax_t));
constexpr auto N = sizeof(Item);
static_assert(N<=Nfixed, "fixed size is too small");
static_assert(N <= Nfixed, "fixed size is too small");
if (Size <= Nfixed + 1) {
return;
}
static_assert(std::is_trivially_copyable<Item>::value, "Item must be blittable");
static_assert(std::is_trivially_copyable<Item>::value,
"Item must be blittable");
Item item{};
std::memcpy(&item, Data, N);

//fast forward
// fast forward
Data += Nfixed;
Size -= Nfixed;

// Data is already allocated separately in libFuzzer so reading past
// the end will most likely be detected anyway
const auto formatstring=fmt::string_view((const char*)Data, Size);
const auto formatstring = fmt::string_view((const char*)Data, Size);

// doit_impl<Item,std::yocto>(buf.data(),item);
// doit_impl<Item,std::zepto>(buf.data(),item);
switch (scaling) {
case 1:
invoke_inner<Item, std::atto>(formatstring, item);
break;
invoke_inner<Item, std::atto>(formatstring, item);
break;
case 2:
invoke_inner<Item, std::femto>(formatstring, item);
break;
invoke_inner<Item, std::femto>(formatstring, item);
break;
case 3:
invoke_inner<Item, std::pico>(formatstring, item);
break;
invoke_inner<Item, std::pico>(formatstring, item);
break;
case 4:
invoke_inner<Item, std::nano>(formatstring, item);
break;
invoke_inner<Item, std::nano>(formatstring, item);
break;
case 5:
invoke_inner<Item, std::micro>(formatstring, item);
break;
invoke_inner<Item, std::micro>(formatstring, item);
break;
case 6:
invoke_inner<Item, std::milli>(formatstring, item);
break;
invoke_inner<Item, std::milli>(formatstring, item);
break;
case 7:
invoke_inner<Item, std::centi>(formatstring, item);
break;
invoke_inner<Item, std::centi>(formatstring, item);
break;
case 8:
invoke_inner<Item, std::deci>(formatstring, item);
break;
invoke_inner<Item, std::deci>(formatstring, item);
break;
case 9:
invoke_inner<Item, std::deca>(formatstring, item);
break;
invoke_inner<Item, std::deca>(formatstring, item);
break;
case 10:
invoke_inner<Item, std::kilo>(formatstring, item);
break;
invoke_inner<Item, std::kilo>(formatstring, item);
break;
case 11:
invoke_inner<Item, std::mega>(formatstring, item);
break;
invoke_inner<Item, std::mega>(formatstring, item);
break;
case 12:
invoke_inner<Item, std::giga>(formatstring, item);
break;
invoke_inner<Item, std::giga>(formatstring, item);
break;
case 13:
invoke_inner<Item, std::tera>(formatstring, item);
break;
invoke_inner<Item, std::tera>(formatstring, item);
break;
case 14:
invoke_inner<Item, std::peta>(formatstring, item);
break;
invoke_inner<Item, std::peta>(formatstring, item);
break;
case 15:
invoke_inner<Item, std::exa>(formatstring, item);
invoke_inner<Item, std::exa>(formatstring, item);
}
// doit_impl<Item,std::zeta>(buf.data(),item);
// doit_impl<Item,std::yotta>(buf.data(),item);
@@ -111,8 +112,8 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, std::size_t Size) {

const auto representation = Data[0];
const auto scaling = Data[1];
Data+=2;
Size-=2;
Data += 2;
Size -= 2;

switch (representation) {
case 1:
32 changes: 16 additions & 16 deletions test/fuzzing/named_arg.cpp
Original file line number Diff line number Diff line change
@@ -29,15 +29,15 @@ void invoke_fmt(const uint8_t* Data, std::size_t Size, int argsize) {
return;
}

//allocating buffers separately is slower, but increases chances
//of detecting memory errors
// allocating buffers separately is slower, but increases chances
// of detecting memory errors
#define SEPARATE_ALLOCATION 1
#if SEPARATE_ALLOCATION
std::vector<char> argnamebuffer(argsize);
std::memcpy(argnamebuffer.data(), Data, argsize);
auto argname=fmt::string_view(argnamebuffer.data(),argsize);
#else
auto argname=fmt::string_view((const char*)Data,argsize);
auto argname = fmt::string_view(argnamebuffer.data(), argsize);
#else
auto argname = fmt::string_view((const char*)Data, argsize);
#endif
Data += argsize;
Size -= argsize;
@@ -46,38 +46,36 @@ void invoke_fmt(const uint8_t* Data, std::size_t Size, int argsize) {
// allocates as tight as possible, making it easier to catch buffer overruns.
std::vector<char> fmtstringbuffer(Size);
std::memcpy(fmtstringbuffer.data(), Data, Size);
auto fmtstring=fmt::string_view(fmtstringbuffer.data(),Size);
auto fmtstring = fmt::string_view(fmtstringbuffer.data(), Size);
#else
auto fmtstring=fmt::string_view((const char*)Data,Size);
auto fmtstring = fmt::string_view((const char*)Data, Size);
#endif
std::string message =
fmt::format(fmtstring, fmt::arg(argname, item1));
std::string message = fmt::format(fmtstring, fmt::arg(argname, item1));
#undef SEPARATE_ALLOCATION
}


// for dynamic dispatching to an explicit instantiation
template <typename Callback> void invoke(int index, Callback callback) {
switch (index) {
case 0:
callback(bool{});
break;
case 1:
callback(char{});
callback(char{});
break;
case 11:
using sc=signed char;
using sc = signed char;
callback(sc{});
break;
case 21:
using uc=unsigned char;
using uc = unsigned char;
callback(uc{});
break;
case 2:
callback(short{});
break;
case 22:
using us=unsigned short;
using us = unsigned short;
callback(us{});
break;
case 3:
@@ -90,7 +88,7 @@ template <typename Callback> void invoke(int index, Callback callback) {
callback(long{});
break;
case 14:
using ul=unsigned long;
using ul = unsigned long;
callback(ul{});
break;
case 5:
@@ -117,7 +115,9 @@ extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, std::size_t Size) {
Data++;
Size--;

auto outerfcn = [=](auto param1) { invoke_fmt<decltype(param1)>(Data, Size, second); };
auto outerfcn = [=](auto param1) {
invoke_fmt<decltype(param1)>(Data, Size, second);
};

try {
invoke(first, outerfcn);

0 comments on commit a667365

Please sign in to comment.