Skip to content

Commit

Permalink
No public description
Browse files Browse the repository at this point in the history
PiperOrigin-RevId: 598898824
  • Loading branch information
hadi88 authored and copybara-github committed Jan 16, 2024
1 parent f7a24ff commit 895ea48
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 5 deletions.
7 changes: 7 additions & 0 deletions fuzztest/fuzztest_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -126,6 +127,12 @@ inline std::vector<std::tuple<std::string>> ReadFilesFromDirectory(
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_
3 changes: 2 additions & 1 deletion fuzztest/internal/domains/container_of_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -247,7 +247,8 @@ class ContainerOfImplBase
}

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 {
Expand Down
11 changes: 7 additions & 4 deletions fuzztest/internal/type_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -172,9 +172,11 @@ struct StringPrinter {
// 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);
Expand Down Expand Up @@ -584,7 +586,8 @@ decltype(auto) AutodetectTypePrinter() {
} 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{};
Expand Down
7 changes: 7 additions & 0 deletions fuzztest/internal/type_support_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
#include <array>
#include <cmath>
#include <complex>
#include <cstdint>
#include <limits>
#include <list>
#include <map>
Expand Down Expand Up @@ -193,6 +194,12 @@ TEST(StringTest, Printer) {
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>>()),
Expand Down

0 comments on commit 895ea48

Please sign in to comment.