Skip to content

Commit

Permalink
Handle functions outside namespaces in GetFunctionName().
Browse files Browse the repository at this point in the history
In the debug mode, the string containing the symbolized function name starts
with the path to the source file and the line number of the function. If the
function is not in an anonymous namespace, those incorrectly get printed out
as part of the function name.

PiperOrigin-RevId: 679602262
  • Loading branch information
fniksic authored and copybara-github committed Sep 27, 2024
1 parent 9bca69d commit f0177b9
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 0 deletions.
26 changes: 26 additions & 0 deletions fuzztest/internal/type_support.h
Original file line number Diff line number Diff line change
Expand Up @@ -335,6 +335,31 @@ constexpr bool HasFunctionName() {
return std::is_function_v<std::remove_pointer_t<F>>;
}

inline void ConsumeFileAndLineNumber(absl::string_view& v) {
// We're essentially matching the regexp "[^:]\:\d+ ", but manually since we
// don't want to introduce a dependency on RE2.
absl::string_view::size_type pos = 0;
while (pos < v.size()) {
pos = v.find(':', pos);
if (pos == v.npos) return;
// Skip the colon.
++pos;
if (pos >= v.size() || !std::isdigit(v[pos])) {
// Colon not followed by a digit. Skip any subsequent colons and continue.
while (pos < v.size() && v[pos] == ':') ++pos;
continue;
}
// Skip the digits.
++pos;
while (pos < v.size() && std::isdigit(v[pos])) ++pos;
if (pos >= v.size() || v[pos] != ' ') continue;
// Skip the space.
++pos;
v.remove_prefix(pos);
return;
}
}

template <typename F>
std::string GetFunctionName(const F& f, absl::string_view default_name) {
if constexpr (HasFunctionName<F>()) {
Expand All @@ -343,6 +368,7 @@ std::string GetFunctionName(const F& f, absl::string_view default_name) {
sizeof(buffer))) {
absl::string_view v = buffer;
absl::ConsumeSuffix(&v, "()");
ConsumeFileAndLineNumber(v);
SkipAnonymous(v);
return std::string(v);
}
Expand Down
20 changes: 20 additions & 0 deletions fuzztest/internal/type_support_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -383,6 +383,26 @@ TEST(MapTest, Printer) {
Each("\"0x15\""));
}

} // namespace
} // namespace fuzztest::internal

auto DoubleValueOutsideNamespace(int n) { return 2 * n; }

namespace fuzztest::internal {
namespace {

TEST(MapTest, PrintsMapperOutsideNamespace) {
auto domain = Map(DoubleValueOutsideNamespace, InRange(2, 5));
std::tuple<int> corpus_value(3);

EXPECT_THAT(
TestPrintValue(corpus_value, domain),
ElementsAre("6",
// Takes into account that the function name may
// contain ABI annotations after de-mangling.
MatchesRegex(R"re(DoubleValueOutsideNamespace.*\(3\))re")));
}

auto ValueInRange(int a, int b) {
int min = std::min(a, b);
int max = std::max(a, b);
Expand Down

0 comments on commit f0177b9

Please sign in to comment.