cjson_read_fuzzer.cc 846 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. #include <stdlib.h>
  2. #include <stdint.h>
  3. #include "../cJSON.h"
  4. extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
  5. {
  6. if((data[0] == '\0') || (size < 3) || (data[1] == '\0')) return 0;
  7. cJSON *json = cJSON_Parse((const char*)data + 2);
  8. if(json == NULL) return 0;
  9. int do_format = 0;
  10. char *printed_json = NULL;
  11. if(data[1] == 'f') do_format = 1;
  12. if(data[0] == 'b')
  13. {
  14. /* buffered printing */
  15. printed_json = cJSON_PrintBuffered(json, 1, do_format);
  16. }
  17. else
  18. {
  19. /* unbuffered printing */
  20. if(do_format)
  21. {
  22. printed_json = cJSON_Print(json);
  23. }
  24. else
  25. {
  26. printed_json = cJSON_PrintUnformatted(json);
  27. }
  28. }
  29. if(printed_json != NULL) free(printed_json);
  30. cJSON_Delete(json);
  31. return 0;
  32. }