No public description PiperOrigin-RevId: 598898824
diff --git a/fuzztest/fuzztest_macros.h b/fuzztest/fuzztest_macros.h index 6228e2f..172d0f6 100644 --- a/fuzztest/fuzztest_macros.h +++ b/fuzztest/fuzztest_macros.h
@@ -15,6 +15,7 @@ #ifndef FUZZTEST_FUZZTEST_FUZZTEST_MACROS_H_ #define FUZZTEST_FUZZTEST_FUZZTEST_MACROS_H_ +#include <cstdint> #include <string> #include <string_view> #include <tuple> @@ -126,6 +127,12 @@ return internal::ReadFilesFromDirectory({dir.data(), dir.size()}); } +// Converts string_view into a byte-array, useful when working with the LLVM +// fuzzer interfaces. +inline std::vector<uint8_t> ToByteArray(std::string_view str) { + return std::vector<uint8_t>(str.begin(), str.end()); +} + } // namespace fuzztest #endif // FUZZTEST_FUZZTEST_FUZZTEST_MACROS_H_
diff --git a/fuzztest/internal/domains/container_of_impl.h b/fuzztest/internal/domains/container_of_impl.h index db59c66..2b22a11 100644 --- a/fuzztest/internal/domains/container_of_impl.h +++ b/fuzztest/internal/domains/container_of_impl.h
@@ -247,7 +247,8 @@ } auto GetPrinter() const { - if constexpr (std::is_same_v<value_type, std::string>) { + if constexpr (std::is_same_v<value_type, std::string> || + std::is_same_v<value_type, std::vector<uint8_t>>) { // std::string has special handling for better output return StringPrinter{}; } else {
diff --git a/fuzztest/internal/type_support.h b/fuzztest/internal/type_support.h index fb0e8ed..f09a1d7 100644 --- a/fuzztest/internal/type_support.h +++ b/fuzztest/internal/type_support.h
@@ -23,7 +23,7 @@ #include <string_view> #include <tuple> #include <type_traits> -#include <utility> +#include <vector> #include "absl/debugging/symbolize.h" #include "absl/numeric/int128.h" @@ -172,9 +172,11 @@ // Make sure to properly C-escape strings when printing source code, and // explicitly construct a std::string of the right length if there is an // embedded NULL character. - const absl::string_view input(v.data(), v.size()); + const std::string input(v.data(), v.data() + v.size()); const std::string escaped = absl::CEscape(input); - if (absl::StrContains(input, '\0')) { + if constexpr (std::is_convertible_v<T, std::vector<uint8_t>>) { + absl::Format(out, "fuzztest::ToByteArray(\"%s\")", escaped); + } else if (absl::StrContains(input, '\0')) { absl::Format(out, "std::string(\"%s\", %d)", escaped, v.size()); } else { absl::Format(out, "\"%s\"", escaped); @@ -584,7 +586,8 @@ } else if constexpr (std::is_floating_point_v<T>) { return FloatingPrinter{}; } else if constexpr (std::is_convertible_v<T, absl::string_view> || - std::is_convertible_v<T, std::string_view>) { + std::is_convertible_v<T, std::string_view> || + std::is_convertible_v<T, std::vector<uint8_t>>) { return StringPrinter{}; } else if constexpr (is_monostate_v<T>) { return MonostatePrinter{};
diff --git a/fuzztest/internal/type_support_test.cc b/fuzztest/internal/type_support_test.cc index fe2c3f0..13df5f9 100644 --- a/fuzztest/internal/type_support_test.cc +++ b/fuzztest/internal/type_support_test.cc
@@ -18,6 +18,7 @@ #include <array> #include <cmath> #include <complex> +#include <cstdint> #include <limits> #include <list> #include <map> @@ -193,6 +194,12 @@ R"("printf(\"Hello, world!\");")")); } +TEST(ByteArrayTest, Printer) { + EXPECT_THAT(TestPrintValue(std::vector<uint8_t>{'\0', 'a', 0223, 'b', '\"'}), + ElementsAre(R"("\000a\223b"")", + R"(fuzztest::ToByteArray("\000a\223b\""))")); +} + TEST(CompoundTest, Printer) { EXPECT_THAT( TestPrintValue(std::pair(1, 1.5), Arbitrary<std::pair<int, double>>()),