refactored kill signal env parsing
MODIFY
include/common.h
include/common.h
@@ -56,6 +56,11 @@ extern u8 *doc_path; /* path to documentation dir */
u8 *find_binary(u8 *fname);
+/* Parses the kill signal environment variable, FATALs on error.
+ If the env is not set, sets the env to default_signal for the signal handlers
+ and returns the default_signal. */
+int parse_afl_kill_signal_env(u8 *afl_kill_signal_env, int default_signal);
+
/* Read a bitmap from file fname to memory
This is for the -B option again. */

MODIFY
src/afl-common.c
src/afl-common.c
@@ -424,6 +424,40 @@ u8 *find_binary(u8 *fname) {
}
+/* Parses the kill signal environment variable, FATALs on error.
+ If the env is not set, sets the env to default_signal for the signal handlers
+ and returns the default_signal. */
+int parse_afl_kill_signal_env(u8 *afl_kill_signal_env, int default_signal) {
+
+ if (afl_kill_signal_env && afl_kill_signal_env[0]) {
+
+ char *endptr;
+ u8 signal_code;
+ signal_code = (u8)strtoul(afl_kill_signal_env, &endptr, 10);
+ /* Did we manage to parse the full string? */
+ if (*endptr != '\0' || endptr == (char *)afl_kill_signal_env) {
+
+ FATAL("Invalid AFL_KILL_SIGNAL: %s (expected unsigned int)",
+ afl_kill_signal_env);
+
+ }
+
+ return signal_code;
+
+ } else {
+
+ char *sigstr = alloc_printf("%d", default_signal);
+ if (!sigstr) { FATAL("Failed to alloc mem for signal buf"); }
+
+ /* Set the env for signal handler */
+ setenv("AFL_KILL_SIGNAL", sigstr, 1);
+ free(sigstr);
+ return default_signal;
+
+ }
+
+}
+
void check_environment_vars(char **envp) {
if (be_quiet) { return; }

MODIFY
src/afl-fuzz.c
src/afl-fuzz.c
@@ -76,9 +76,9 @@ static void at_exit() {
}
- u8 kill_signal = SIGKILL;
+ int kill_signal = SIGKILL;
- /* AFL_KILL_SIGNAL should already be initialized by afl_fsrv_init() */
+ /* AFL_KILL_SIGNAL should already be a valid int at this point */
if (getenv("AFL_KILL_SIGNAL")) {
kill_signal = atoi(getenv("AFL_KILL_SIGNAL"));
@@ -987,32 +987,8 @@ int main(int argc, char **argv_orig, char **envp) {
#endif
- afl->fsrv.kill_signal = SIGKILL;
- if (afl->afl_env.afl_kill_signal) {
-
- char *endptr;
- u8 signal_code;
- signal_code = (u8)strtoul(afl->afl_env.afl_kill_signal, &endptr, 10);
- /* Did we manage to parse the full string? */
- if (*endptr != '\0' || endptr == (char *)afl->afl_env.afl_kill_signal) {
-
- FATAL("Invalid AFL_KILL_SIGNAL: %s (expected unsigned int)",
- afl->afl_env.afl_kill_signal);
-
- }
-
- afl->fsrv.kill_signal = signal_code;
-
- } else {
-
- char *sigstr = alloc_printf("%d", (int)SIGKILL);
- if (!sigstr) { FATAL("Failed to alloc mem for signal buf"); }
-
- /* Set the env for signal handler */
- setenv("AFL_KILL_SIGNAL", sigstr, 1);
- free(sigstr);
-
- }
+ afl->fsrv.kill_signal =
+ parse_afl_kill_signal_env(afl->afl_env.afl_kill_signal, SIGKILL);
setup_signal_handlers();
check_asan_opts(afl);

MODIFY
src/afl-showmap.c
src/afl-showmap.c
@@ -1116,33 +1116,8 @@ int main(int argc, char **argv_orig, char **envp) {
}
- fsrv->kill_signal = SIGKILL;
- char *afl_kill_signal_env = getenv("AFL_KILL_SIGNAL");
- if (afl_kill_signal_env && afl_kill_signal_env[0]) {
-
- char *endptr;
- u8 signal_code;
- signal_code = (u8)strtoul(afl_kill_signal_env, &endptr, 10);
- /* Did we manage to parse the full string? */
- if (*endptr != '\0' || endptr == afl_kill_signal_env) {
-
- FATAL("Invalid AFL_KILL_SIGNAL: %s (expected unsigned int)",
- afl_kill_signal_env);
-
- }
-
- fsrv->kill_signal = signal_code;
-
- } else {
-
- char *sigstr = alloc_printf("%d", (int)SIGKILL);
- if (!sigstr) { FATAL("Failed to alloc mem for signal buf"); }
-
- /* Set the env for signal handler */
- setenv("AFL_KILL_SIGNAL", sigstr, 1);
- free(sigstr);
-
- }
+ fsrv->kill_signal =
+ parse_afl_kill_signal_env(getenv("AFL_KILL_SIGNAL"), SIGKILL);
if (getenv("AFL_CRASH_EXITCODE")) {

MODIFY
src/afl-tmin.c
src/afl-tmin.c
@@ -1135,33 +1135,8 @@ int main(int argc, char **argv_orig, char **envp) {
}
- fsrv->kill_signal = SIGKILL;
- char *afl_kill_signal_env = getenv("AFL_KILL_SIGNAL");
- if (afl_kill_signal_env && afl_kill_signal_env[0]) {
-
- char *endptr;
- u8 signal_code;
- signal_code = (u8)strtoul(afl_kill_signal_env, &endptr, 10);
- /* Did we manage to parse the full string? */
- if (*endptr != '\0' || endptr == afl_kill_signal_env) {
-
- FATAL("Invalid AFL_KILL_SIGNAL: %s (expected unsigned int)",
- afl_kill_signal_env);
-
- }
-
- fsrv->kill_signal = signal_code;
-
- } else {
-
- char *sigstr = alloc_printf("%d", (int)SIGKILL);
- if (!sigstr) { FATAL("Failed to alloc mem for signal buf"); }
-
- /* Set the env for signal handler */
- setenv("AFL_KILL_SIGNAL", sigstr, 1);
- free(sigstr);
-
- }
+ fsrv->kill_signal =
+ parse_afl_kill_signal_env(getenv("AFL_KILL_SIGNAL"), SIGKILL);
if (getenv("AFL_CRASH_EXITCODE")) {

refactored kill signal env parsing