Skip to content

Commit fa22966

Browse files
author
MarcoFalke
committed
fuzz: Print error message when FUZZ is missing
Also, add missing includes.
1 parent 17acb27 commit fa22966

File tree

2 files changed

+22
-7
lines changed

2 files changed

+22
-7
lines changed

src/test/fuzz/fuzz.cpp

Lines changed: 21 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,19 @@
1414

1515
#include <csignal>
1616
#include <cstdint>
17+
#include <cstdio>
18+
#include <cstdlib>
19+
#include <cstring>
1720
#include <exception>
1821
#include <fstream>
1922
#include <functional>
23+
#include <iostream>
2024
#include <map>
2125
#include <memory>
2226
#include <string>
2327
#include <tuple>
2428
#include <unistd.h>
29+
#include <utility>
2530
#include <vector>
2631

2732
const std::function<void(const std::string&)> G_TEST_LOG_FUN{};
@@ -77,13 +82,13 @@ void initialize()
7782
return WrappedGetAddrInfo(name, false);
7883
};
7984

80-
bool should_abort{false};
85+
bool should_exit{false};
8186
if (std::getenv("PRINT_ALL_FUZZ_TARGETS_AND_ABORT")) {
8287
for (const auto& t : FuzzTargets()) {
8388
if (std::get<2>(t.second)) continue;
8489
std::cout << t.first << std::endl;
8590
}
86-
should_abort = true;
91+
should_exit = true;
8792
}
8893
if (const char* out_path = std::getenv("WRITE_ALL_FUZZ_TARGETS_AND_ABORT")) {
8994
std::cout << "Writing all fuzz target names to '" << out_path << "'." << std::endl;
@@ -92,13 +97,23 @@ void initialize()
9297
if (std::get<2>(t.second)) continue;
9398
out_stream << t.first << std::endl;
9499
}
95-
should_abort = true;
100+
should_exit= true;
101+
}
102+
if (should_exit){
103+
std::exit(EXIT_SUCCESS);
104+
}
105+
if (const auto* env_fuzz{std::getenv("FUZZ")}) {
106+
// To allow for easier fuzz executable binary modification,
107+
static std::string g_copy{env_fuzz}; // create copy to avoid compiler optimizations, and
108+
g_fuzz_target = g_copy.c_str(); // strip string after the first null-char.
109+
} else {
110+
std::cerr << "Must select fuzz target with the FUZZ env var." << std::endl;
111+
std::cerr << "Hint: Set the PRINT_ALL_FUZZ_TARGETS_AND_ABORT=1 env var to see all compiled targets." << std::endl;
112+
std::exit(EXIT_FAILURE);
96113
}
97-
Assert(!should_abort);
98-
g_fuzz_target = Assert(std::getenv("FUZZ"));
99114
const auto it = FuzzTargets().find(g_fuzz_target);
100115
if (it == FuzzTargets().end()) {
101-
std::cerr << "No fuzzer for " << g_fuzz_target << "." << std::endl;
116+
std::cerr << "No fuzz target compiled for " << g_fuzz_target << "." << std::endl;
102117
std::exit(EXIT_FAILURE);
103118
}
104119
Assert(!g_test_one_input);

src/util/fs.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
#include <tinyformat.h>
99

1010
#include <cstdio>
11-
#include <filesystem>
11+
#include <filesystem> // IWYU pragma: export
1212
#include <functional>
1313
#include <iomanip>
1414
#include <ios>

0 commit comments

Comments
 (0)