fuzz: Skip the tests if fmemopen() is not found

fmemopen() is a function that is only provided with *NIX systems, so we
ought to check for its presence in order to build and run the tests
in fuzz/ fully, otherwise, we just skip the tests.

Also include headers according to how they are found, and add fallbacks
for Visual Studio that do not have stdint.h yet.
This commit is contained in:
Chun-wei Fan 2018-04-20 14:42:30 +08:00 committed by Tim Rühsen
parent 44256b1a3a
commit 9620b13374
6 changed files with 45 additions and 4 deletions

View File

@ -312,7 +312,7 @@ AC_SUBST(PSL_TESTFILE)
# check for alloca / alloca.h # check for alloca / alloca.h
AC_FUNC_ALLOCA AC_FUNC_ALLOCA
AC_CHECK_FUNCS([strndup clock_gettime]) AC_CHECK_FUNCS([strndup clock_gettime fmemopen])
# Override the template file name of the generated .pc file, so that there # Override the template file name of the generated .pc file, so that there
# is no need to rename the template file when the API version changes. # is no need to rename the template file when the API version changes.

View File

@ -22,8 +22,15 @@
* This file is part of libpsl. * This file is part of libpsl.
*/ */
#include <config.h>
#include <stddef.h> /* size_t */ #include <stddef.h> /* size_t */
#ifdef HAVE_STDINT_H
#include <stdint.h> /* uint8_t */ #include <stdint.h> /* uint8_t */
#elif defined (_MSC_VER)
typedef unsigned __int8 uint8_t;
#endif
#ifdef __cplusplus #ifdef __cplusplus
extern "C" extern "C"

View File

@ -25,7 +25,13 @@
#include <config.h> #include <config.h>
#include <assert.h> /* assert */ #include <assert.h> /* assert */
#ifdef HAVE_STDINT_H
#include <stdint.h> /* uint8_t */ #include <stdint.h> /* uint8_t */
#elif defined (_MSC_VER)
typedef unsigned __int8 uint8_t;
#endif
#include <stdlib.h> /* malloc, free */ #include <stdlib.h> /* malloc, free */
#include <string.h> /* memcpy */ #include <string.h> /* memcpy */

View File

@ -25,7 +25,13 @@
#include <config.h> #include <config.h>
#include <assert.h> /* assert */ #include <assert.h> /* assert */
#ifdef HAVE_STDINT_H
#include <stdint.h> /* uint8_t */ #include <stdint.h> /* uint8_t */
#elif defined (_MSC_VER)
typedef unsigned __int8 uint8_t;
#endif
#include <stdlib.h> /* malloc, free */ #include <stdlib.h> /* malloc, free */
#include <string.h> /* memcpy */ #include <string.h> /* memcpy */
@ -34,6 +40,7 @@
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{ {
#ifdef HAVE_FMEMOPEN
FILE *fp; FILE *fp;
psl_ctx_t *psl; psl_ctx_t *psl;
char *in = (char *) malloc(size + 16); char *in = (char *) malloc(size + 16);
@ -62,6 +69,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
psl_free(psl); psl_free(psl);
free(in); free(in);
#endif
return 0; return 0;
} }

View File

@ -25,7 +25,13 @@
#include <config.h> #include <config.h>
#include <assert.h> /* assert */ #include <assert.h> /* assert */
#ifdef HAVE_STDINT_H
#include <stdint.h> /* uint8_t */ #include <stdint.h> /* uint8_t */
#elif defined (_MSC_VER)
typedef unsigned __int8 uint8_t;
#endif
#include <stdlib.h> /* malloc, free */ #include <stdlib.h> /* malloc, free */
#include <string.h> /* memcpy */ #include <string.h> /* memcpy */
@ -34,6 +40,7 @@
int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{ {
#ifdef HAVE_FMEMOPEN
FILE *fp; FILE *fp;
psl_ctx_t *psl; psl_ctx_t *psl;
@ -50,6 +57,7 @@ int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
fclose(fp); fclose(fp);
psl_load_file("/dev/null"); psl_load_file("/dev/null");
#endif
return 0; return 0;
} }

View File

@ -25,9 +25,17 @@
#include "../config.h" #include "../config.h"
#include <stdio.h> #include <stdio.h>
#ifdef HAVE_UNISTD_H
#include <unistd.h> #include <unistd.h>
#endif
#include <stdlib.h> #include <stdlib.h>
#ifdef HAVE_STDINT_H
#include <stdint.h> #include <stdint.h>
#endif
#include <string.h> #include <string.h>
#include <fcntl.h> #include <fcntl.h>
#include <errno.h> #include <errno.h>
@ -35,7 +43,7 @@
#include "fuzzer.h" #include "fuzzer.h"
#ifdef TEST_RUN #if defined (TEST_RUN) && defined (HAVE_FMEMOPEN)
#include <dirent.h> #include <dirent.h>
#ifdef HAVE_ALLOCA_H #ifdef HAVE_ALLOCA_H
@ -114,7 +122,7 @@ int main(int argc, char **argv)
return 0; return 0;
} }
#else #else /* TEST_RUN && HAVE_FMEMOPEN */
#ifndef __AFL_LOOP #ifndef __AFL_LOOP
static int __AFL_LOOP(int n) static int __AFL_LOOP(int n)
@ -132,6 +140,7 @@ static int __AFL_LOOP(int n)
int main(int argc, char **argv) int main(int argc, char **argv)
{ {
#ifdef HAVE_FMEMOPEN
int ret; int ret;
unsigned char buf[64 * 1024]; unsigned char buf[64 * 1024];
@ -144,6 +153,9 @@ int main(int argc, char **argv)
} }
return 0; return 0;
#else
exit (77);
#endif
} }
#endif /* TEST_RUN */ #endif /* TEST_RUN && HAVE_FMEMOPEN*/