Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adding an overload of ReadFilesFromDirectory that takes in a filename… #1520

Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 29 additions & 0 deletions fuzztest/fuzztest_macros.cc
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,35 @@ std::vector<std::tuple<std::string>> ReadFilesFromDirectory(
return out;
}

std::vector<std::tuple<std::string>> ReadFilesFromDirectory(
std::string_view dir, std::string_view filename_pattern) {
std::vector<std::tuple<std::string>> out;
const std::filesystem::path fs_dir(dir);
std::regex regex_pattern(filename_pattern.data());
if (!std::filesystem::is_directory(fs_dir)) return out;
for (const auto& entry :
std::filesystem::recursive_directory_iterator(fs_dir)) {
if (std::filesystem::is_directory(entry)) continue;

// Check if the file matches regex_pattern
if (!std::regex_match(entry.path().string(), regex_pattern)) continue;

std::ifstream stream(entry.path().string());
if (!stream.good()) {
// Using stderr instead of GetStderr() to avoid
// initialization-order-fiasco when reading files at static init time with
// `.WithSeeds(fuzztest::ReadFilesFromDirectory(...))`.
absl::FPrintF(stderr, "[!] %s:%d: Error reading %s: (%d) %s\n", __FILE__,
__LINE__, entry.path().string(), errno, strerror(errno));
continue;
}
std::stringstream buffer;
buffer << stream.rdbuf();
out.push_back({buffer.str()});
}
return out;
}

absl::StatusOr<std::vector<std::string>> ParseDictionary(
absl::string_view text) {
std::vector<std::string> parsed_entries;
Expand Down
6 changes: 6 additions & 0 deletions fuzztest/fuzztest_macros.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#define FUZZTEST_FUZZTEST_FUZZTEST_MACROS_H_

#include <cstdint>
#include <regex>
#include <string>
#include <string_view>
#include <tuple>
Expand Down Expand Up @@ -127,6 +128,11 @@ namespace fuzztest {
std::vector<std::tuple<std::string>> ReadFilesFromDirectory(
std::string_view dir);

// Overload of ReadFilesFromDirectory() that filters files by their names using regex.
// Example: ReadFilesFromDirectory(dir, ".*\\.der$") to read only .der files.
std::vector<std::tuple<std::string>> ReadFilesFromDirectory(
std::string_view dir, std::string_view filename_pattern);

// Returns parsed dictionary entries from fuzzer dictionary definition in the
// format specified at https://llvm.org/docs/LibFuzzer.html#dictionaries.
// If dictionary is in wrong format, return error status.
Expand Down
Loading