float.cc 1.2 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. // A fuzzer for floating-point formatter.
  2. // For the license information refer to format.h.
  3. #include <fmt/format.h>
  4. #include <cstdint>
  5. #include <cstdlib>
  6. #include <limits>
  7. #include <stdexcept>
  8. #include "fuzzer-common.h"
  9. void check_round_trip(fmt::string_view format_str, double value) {
  10. auto buffer = fmt::memory_buffer();
  11. fmt::format_to(std::back_inserter(buffer), format_str, value);
  12. if (std::isnan(value)) {
  13. auto nan = std::signbit(value) ? "-nan" : "nan";
  14. if (fmt::string_view(buffer.data(), buffer.size()) != nan)
  15. throw std::runtime_error("round trip failure");
  16. return;
  17. }
  18. buffer.push_back('\0');
  19. char* ptr = nullptr;
  20. if (std::strtod(buffer.data(), &ptr) != value)
  21. throw std::runtime_error("round trip failure");
  22. if (ptr + 1 != buffer.end()) throw std::runtime_error("unparsed output");
  23. }
  24. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
  25. if (size <= sizeof(double) || !std::numeric_limits<double>::is_iec559)
  26. return 0;
  27. check_round_trip("{}", assign_from_buf<double>(data));
  28. // A larger than necessary precision is used to trigger the fallback
  29. // formatter.
  30. check_round_trip("{:.50g}", assign_from_buf<double>(data));
  31. return 0;
  32. }