Skip to content

Commit

Permalink
format also void*
Browse files Browse the repository at this point in the history
  • Loading branch information
pauldreik committed Jun 9, 2019
1 parent 820142e commit a0004eb
Showing 1 changed file with 18 additions and 12 deletions.
30 changes: 18 additions & 12 deletions test/fuzzing/sprintf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,18 @@

constexpr auto Nmax = std::max(sizeof(long double), sizeof(std::intmax_t));

template<class Item>
Item assignFromBuf(const uint8_t* Data, std::size_t Size) {
Item item{};
std::memcpy(&item, Data, sizeof(Item));
return item;
}

template<>
bool assignFromBuf<bool>(const uint8_t* Data, std::size_t Size) {
return !!Data[0];
}

template <typename Item1, typename Item2>
void invoke_fmt(const uint8_t* Data, std::size_t Size) {
const auto N1 = sizeof(Item1);
Expand All @@ -20,21 +32,11 @@ void invoke_fmt(const uint8_t* Data, std::size_t Size) {
if (Size <= Nmax + Nmax) {
return;
}
Item1 item1{};
if /*constexpr*/ (std::is_same<Item1, bool>::value) {
item1 = !!Data[0];
} else {
std::memcpy(&item1, Data, N1);
}
Item1 item1=assignFromBuf<Item1>(Data,Size);
Data += Nmax;
Size -= Nmax;

Item2 item2{};
if /*constexpr*/ (std::is_same<Item2, bool>::value) {
item2 = !!Data[0];
} else {
std::memcpy(&item2, Data, N2);
}
Item2 item2=assignFromBuf<Item2>(Data,Size);
Data += Nmax;
Size -= Nmax;

Expand Down Expand Up @@ -96,6 +98,10 @@ template <typename Callback> void invoke(int index, Callback callback) {
using LD = long double;
callback(LD{});
break;
case 13:
using ptr = void*;
callback(ptr{});
break;
}
}

Expand Down

0 comments on commit a0004eb

Please sign in to comment.