diff --git a/fuzzing/chrono.cpp b/fuzzing/chrono.cpp new file mode 100644 index 000000000000..b13346cea422 --- /dev/null +++ b/fuzzing/chrono.cpp @@ -0,0 +1,107 @@ +// Copyright (c) 2019, Paul Dreik +// License: see LICENSE.rst in the fmt root directory + +#include +#include +#include +#include +#include + +#include + +template void doit_impl(const char* formatstring,Item item) { + std::chrono::duration value(item); + std::string message = fmt::format(formatstring, value); +} + +//Item is the underlying type for duration (int, long etc) +template void doit(const uint8_t* Data, std::size_t Size) { + const auto N = sizeof(Item); + if (Size <= N) { + return; + } + Item item{}; + std::memcpy(&item, Data, N); + Data += N; + Size -= N; + + // allocates as tight as possible, making it easier to catch buffer overruns + // also, make it null terminated. + std::vector buf(Size + 1); + std::memcpy(buf.data(), Data, Size); + // doit_impl(buf.data(),item); + // doit_impl(buf.data(),item); + doit_impl(buf.data(),item); + doit_impl(buf.data(),item); + doit_impl(buf.data(),item); + doit_impl(buf.data(),item); + doit_impl(buf.data(),item); + doit_impl(buf.data(),item); + doit_impl(buf.data(),item); + doit_impl(buf.data(),item); + doit_impl(buf.data(),item); + doit_impl(buf.data(),item); + doit_impl(buf.data(),item); doit_impl(buf.data(),item); doit_impl(buf.data(),item); doit_impl(buf.data(),item); doit_impl(buf.data(),item); +// doit_impl(buf.data(),item); doit_impl(buf.data(),item); +} + + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* Data, std::size_t Size) { + if (Size <= 3) { + return 0; + } + + const auto first = Data[0]; + Data++; + Size--; + + try { + switch (first) { + case 1: + doit(Data, Size); + break; + case 2: + doit(Data, Size); + break; + case 3: + doit(Data, Size); + break; + case 4: + doit(Data, Size); + break; + case 5: + doit(Data, Size); + break; + case 6: + doit(Data, Size); + break; + case 7: + doit(Data, Size); + break; + default: + break; + } + } catch (std::exception& e) { + } + return 0; +} + +#ifdef IMPLEMENT_MAIN +# include +# include +# include +# include +int main(int argc, char* argv[]) { + for (int i = 1; i < argc; ++i) { + std::ifstream in(argv[i]); + assert(in); + in.seekg(0, std::ios_base::end); + const auto pos = in.tellg(); + in.seekg(0, std::ios_base::beg); + std::vector buf(pos); + in.read(buf.data(), buf.size()); + assert(in.gcount() == pos); + LLVMFuzzerTestOneInput((const uint8_t*)buf.data(), buf.size()); + } +} +#endif