From 49544ea9432a13285846aca6d191f01e8ed00655 Mon Sep 17 00:00:00 2001 From: Victor Zverovich Date: Tue, 3 Nov 2020 19:34:35 -0800 Subject: [PATCH] Fuzz fallback formatter --- test/fuzzing/float.cc | 21 +++++++++++++-------- 1 file changed, 13 insertions(+), 8 deletions(-) diff --git a/test/fuzzing/float.cc b/test/fuzzing/float.cc index a9a347ef..073e4bcd 100644 --- a/test/fuzzing/float.cc +++ b/test/fuzzing/float.cc @@ -9,26 +9,31 @@ #include "fuzzer-common.h" -extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { - if (size <= sizeof(double) || !std::numeric_limits::is_iec559) - return 0; - - auto value = assign_from_buf(data); +void check_round_trip(fmt::string_view format_str, double value) { auto buffer = fmt::memory_buffer(); - fmt::format_to(buffer, "{}", value); + fmt::format_to(buffer, format_str, value); - // Check a round trip. if (std::isnan(value)) { auto nan = std::signbit(value) ? "-nan" : "nan"; if (fmt::string_view(buffer.data(), buffer.size()) != nan) throw std::runtime_error("round trip failure"); - return 0; + return; } + buffer.push_back('\0'); char* ptr = nullptr; if (std::strtod(buffer.data(), &ptr) != value) throw std::runtime_error("round trip failure"); if (ptr + 1 != buffer.end()) throw std::runtime_error("unparsed output"); +} + +extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) { + if (size <= sizeof(double) || !std::numeric_limits::is_iec559) + return 0; + check_round_trip("{}", assign_from_buf(data)); + // A larger than necessary precision is used to trigger the fallback + // formatter. + check_round_trip("{:.50g}", assign_from_buf(data)); return 0; }