From f45332e1ab1f7b82c38c6fcc3c451fca35d5a9ce Mon Sep 17 00:00:00 2001 From: hexcoder- Date: Sat, 13 Jul 2019 08:09:19 +0200 Subject: portability fix: getcwd(NULL, 0) is a non-POSIX glibc extension. Refactor detect_file_args() in a separate file in order to avoid multiple copies. --- afl-common.c | 69 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 69 insertions(+) create mode 100644 afl-common.c (limited to 'afl-common.c') diff --git a/afl-common.c b/afl-common.c new file mode 100644 index 00000000..ea3bfa6c --- /dev/null +++ b/afl-common.c @@ -0,0 +1,69 @@ +/* + gather some functions common to multiple executables + + detect_file_args + */ + +#include +#include +#include + +#include "debug.h" +#include "alloc-inl.h" + +/* Detect @@ in args. */ +#ifndef __glibc__ +#include +#endif +void detect_file_args(char** argv, u8* prog_in) { + + u32 i = 0; +#ifdef __glibc__ + u8* cwd = getcwd(NULL, 0); /* non portable glibc extension */ +#else + u8* cwd; + char *buf; + long size = pathconf(".", _PC_PATH_MAX); + if ((buf = (char *)malloc((size_t)size)) != NULL) { + cwd = getcwd(buf, (size_t)size); /* portable version */ + } else { + PFATAL("getcwd() failed"); + } +#endif + + if (!cwd) PFATAL("getcwd() failed"); + + while (argv[i]) { + + u8* aa_loc = strstr(argv[i], "@@"); + + if (aa_loc) { + + u8 *aa_subst, *n_arg; + + if (!prog_in) FATAL("@@ syntax is not supported by this tool."); + + /* Be sure that we're always using fully-qualified paths. */ + + if (prog_in[0] == '/') aa_subst = prog_in; + else aa_subst = alloc_printf("%s/%s", cwd, prog_in); + + /* Construct a replacement argv value. */ + + *aa_loc = 0; + n_arg = alloc_printf("%s%s%s", argv[i], aa_subst, aa_loc + 2); + argv[i] = n_arg; + *aa_loc = '@'; + + if (prog_in[0] != '/') ck_free(aa_subst); + + } + + i++; + + } + + free(cwd); /* not tracked */ + +} + -- cgit 1.4.1 From 5c0830f62857bc00d1da386e3d204932f544a6ba Mon Sep 17 00:00:00 2001 From: Hexcoder Date: Sat, 13 Jul 2019 08:42:30 +0200 Subject: fix detection of glibc --- afl-common.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'afl-common.c') diff --git a/afl-common.c b/afl-common.c index ea3bfa6c..1c5e5bfe 100644 --- a/afl-common.c +++ b/afl-common.c @@ -18,7 +18,7 @@ void detect_file_args(char** argv, u8* prog_in) { u32 i = 0; -#ifdef __glibc__ +#ifdef __GLIBC__ u8* cwd = getcwd(NULL, 0); /* non portable glibc extension */ #else u8* cwd; -- cgit 1.4.1