comparison dep/fmt/test/fuzzing/float.cc @ 343:1faa72660932

*: transfer back to cmake from autotools autotools just made lots of things more complicated than they should have and many things broke (i.e. translations)
author Paper <paper@paper.us.eu.org>
date Thu, 20 Jun 2024 05:56:06 -0400
parents
children
comparison
equal deleted inserted replaced
342:adb79bdde329 343:1faa72660932
1 // A fuzzer for floating-point formatter.
2 // For the license information refer to format.h.
3
4 #include <fmt/format.h>
5
6 #include <cstdint>
7 #include <cstdlib>
8 #include <limits>
9 #include <stdexcept>
10
11 #include "fuzzer-common.h"
12
13 void check_round_trip(fmt::string_view format_str, double value) {
14 auto buffer = fmt::memory_buffer();
15 fmt::format_to(std::back_inserter(buffer), format_str, value);
16
17 if (std::isnan(value)) {
18 auto nan = std::signbit(value) ? "-nan" : "nan";
19 if (fmt::string_view(buffer.data(), buffer.size()) != nan)
20 throw std::runtime_error("round trip failure");
21 return;
22 }
23
24 buffer.push_back('\0');
25 char* ptr = nullptr;
26 if (std::strtod(buffer.data(), &ptr) != value)
27 throw std::runtime_error("round trip failure");
28 if (ptr + 1 != buffer.end()) throw std::runtime_error("unparsed output");
29 }
30
31 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
32 if (size <= sizeof(double) || !std::numeric_limits<double>::is_iec559)
33 return 0;
34 check_round_trip("{}", assign_from_buf<double>(data));
35 // A larger than necessary precision is used to trigger the fallback
36 // formatter.
37 check_round_trip("{:.50g}", assign_from_buf<double>(data));
38 return 0;
39 }