From 9f006097255c239188840b589f6e39cbb4476481 Mon Sep 17 00:00:00 2001 From: Paul Dreik Date: Wed, 5 Jun 2019 09:45:23 +0200 Subject: [PATCH] switch to fmt::string_view and workaround reported bug --- fuzzing/chrono_duration.cpp | 52 +++++++++++++++++++++++-------------- 1 file changed, 32 insertions(+), 20 deletions(-) diff --git a/fuzzing/chrono_duration.cpp b/fuzzing/chrono_duration.cpp index 4b1be653360f..07dd19a133b3 100644 --- a/fuzzing/chrono_duration.cpp +++ b/fuzzing/chrono_duration.cpp @@ -7,11 +7,10 @@ #include #include #include - #include template -void doit_impl(const char* formatstring, const Item item) { +void doit_impl(fmt::string_view formatstring, const Item item) { const std::chrono::duration value(item); try { std::string message = fmt::format(formatstring, value); @@ -31,6 +30,7 @@ void doit(const uint8_t* Data, std::size_t Size, const int scaling) { if (Size <= Nfixed + 1) { return; } + static_assert(std::is_trivially_constructible::value,"Item must be blittable"); Item item{}; std::memcpy(&item, Data, N); @@ -49,57 +49,69 @@ void doit(const uint8_t* Data, std::size_t Size, const int scaling) { } } - // allocates as tight as possible, making it easier to catch buffer overruns - // also, make it null terminated. - std::vector buf(Size + 1); + // Data is already allocated separately in libFuzzer so reading past + // the end will most likely be detected anyway + + // see https://github.com/fmtlib/fmt/issues/1194 +#define GITHUB_1194_IS_SOLVED 0 +#if GITHUB_1194_IS_SOLVED + const auto formatstring=fmt::string_view((const char*)Data, Size); +#else + // needs a null terminator, so allocate separately + std::vector buf(Size+1); std::memcpy(buf.data(), Data, Size); + const auto formatstring=fmt::string_view(buf.data(), Size); +#endif + + + // doit_impl(buf.data(),item); // doit_impl(buf.data(),item); switch (scaling) { case 1: - doit_impl(buf.data(), item); + doit_impl(formatstring, item); break; case 2: - doit_impl(buf.data(), item); + doit_impl(formatstring, item); break; case 3: - doit_impl(buf.data(), item); + doit_impl(formatstring, item); break; case 4: - doit_impl(buf.data(), item); + doit_impl(formatstring, item); break; case 5: - doit_impl(buf.data(), item); + doit_impl(formatstring, item); break; case 6: - doit_impl(buf.data(), item); + doit_impl(formatstring, item); break; case 7: - doit_impl(buf.data(), item); + doit_impl(formatstring, item); break; case 8: - doit_impl(buf.data(), item); + doit_impl(formatstring, item); break; case 9: - doit_impl(buf.data(), item); + doit_impl(formatstring, item); break; case 10: - doit_impl(buf.data(), item); + doit_impl(formatstring, item); break; case 11: - doit_impl(buf.data(), item); + doit_impl(formatstring, item); break; case 12: - doit_impl(buf.data(), item); + doit_impl(formatstring, item); break; case 13: - doit_impl(buf.data(), item); + doit_impl(formatstring, item); break; case 14: - doit_impl(buf.data(), item); + doit_impl(formatstring, item); break; case 15: - doit_impl(buf.data(), item); + doit_impl(formatstring, item); } // doit_impl(buf.data(),item); // doit_impl(buf.data(),item);