From e6918187568dbd01842d8d1d2c808ce16a894239 Mon Sep 17 00:00:00 2001 From: Daniel Baumann Date: Sun, 21 Apr 2024 13:54:28 +0200 Subject: Adding upstream version 18.2.2. Signed-off-by: Daniel Baumann --- src/fmt/test/fuzzing/float.cc | 39 +++++++++++++++++++++++++++++++++++++++ 1 file changed, 39 insertions(+) create mode 100644 src/fmt/test/fuzzing/float.cc (limited to 'src/fmt/test/fuzzing/float.cc') diff --git a/src/fmt/test/fuzzing/float.cc b/src/fmt/test/fuzzing/float.cc new file mode 100644 index 000000000..d4c9e4f57 --- /dev/null +++ b/src/fmt/test/fuzzing/float.cc @@ -0,0 +1,39 @@ +// A fuzzer for floating-point formatter. +// For the license information refer to format.h. + +#include + +#include +#include +#include +#include + +#include "fuzzer-common.h" + +void check_round_trip(fmt::string_view format_str, double value) { + auto buffer = fmt::memory_buffer(); + fmt::format_to(std::back_inserter(buffer), format_str, value); + + 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; + } + + 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; +} -- cgit v1.2.3