You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

json_pointer_fuzzer.cc 1.5 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253
  1. #include <fuzzer/FuzzedDataProvider.h>
  2. #include "json.h"
  3. extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
  4. FuzzedDataProvider fdp(data, size);
  5. struct json_tokener *tokener =
  6. json_tokener_new_ex(fdp.ConsumeIntegralInRange<int>(1, JSON_TOKENER_DEFAULT_DEPTH));
  7. int flags = 0;
  8. if (fdp.ConsumeBool()) {
  9. flags |= JSON_TOKENER_VALIDATE_UTF8;
  10. }
  11. if (fdp.ConsumeBool()) {
  12. flags |= JSON_TOKENER_ALLOW_TRAILING_CHARS;
  13. }
  14. if (fdp.ConsumeBool()) {
  15. flags |= JSON_TOKENER_STRICT;
  16. }
  17. json_tokener_set_flags(tokener, flags);
  18. std::string path = fdp.ConsumeRandomLengthString(5);
  19. std::string sub_json_str = fdp.ConsumeRandomLengthString(10);
  20. bool use_format_string = fdp.ConsumeBool();
  21. std::string json_str = fdp.ConsumeRemainingBytesAsString();
  22. struct json_object *jo1 = json_tokener_parse_ex(tokener, json_str.c_str(), json_str.size());
  23. struct json_object *sub_json = json_tokener_parse(sub_json_str.c_str());
  24. if (sub_json == NULL) {
  25. sub_json = json_object_new_object();
  26. }
  27. struct json_object *jo2 = NULL;
  28. if (use_format_string) {
  29. json_pointer_getf(jo1, &jo2, "%s", path.c_str());
  30. if (json_pointer_setf(&jo1, sub_json, "%s", path.c_str()))
  31. {
  32. json_object_put(sub_json);
  33. }
  34. } else {
  35. json_pointer_get(jo1, path.c_str(), &jo2);
  36. if (json_pointer_set(&jo1, path.c_str(), sub_json))
  37. {
  38. json_object_put(sub_json);
  39. }
  40. }
  41. // Clean up the main JSON object
  42. json_object_put(jo1);
  43. json_tokener_free(tokener);
  44. return 0;
  45. }