test-fuzz.c 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. #define _GNU_SOURCE
  2. #include <stdio.h>
  3. #include <stdint.h>
  4. #include <stdlib.h>
  5. #include <stddef.h>
  6. #include <string.h>
  7. #include <fcntl.h>
  8. #include <errno.h>
  9. #include <unistd.h>
  10. #include <sys/types.h>
  11. #include <sys/stat.h>
  12. #include "uci.h"
  13. static void fuzz_uci_import(const uint8_t *input, size_t size)
  14. {
  15. int r;
  16. int fd;
  17. FILE *fs = NULL;
  18. struct uci_context *ctx = NULL;
  19. struct uci_package *package = NULL;
  20. fd = open("/dev/shm", O_TMPFILE | O_RDWR, S_IRUSR | S_IWUSR);
  21. if (fd < 0) {
  22. perror("unable to create temp file");
  23. exit(-1);
  24. }
  25. r = write(fd, input, size);
  26. if (r < 0) {
  27. perror("unable to write()");
  28. exit(-1);
  29. }
  30. fs = fdopen(fd, "r");
  31. if (fs == NULL) {
  32. perror("unable to fdopen()");
  33. exit(-1);
  34. }
  35. fseek(fs, 0L, SEEK_SET);
  36. ctx = uci_alloc_context();
  37. if (ctx == NULL) {
  38. perror("unable to uci_alloc_context()");
  39. exit(-1);
  40. }
  41. uci_import(ctx, fs, NULL, &package, false);
  42. uci_free_context(ctx);
  43. close(fd);
  44. fclose(fs);
  45. }
  46. int LLVMFuzzerTestOneInput(const uint8_t *input, size_t size)
  47. {
  48. fuzz_uci_import(input, size);
  49. return 0;
  50. }