aboutsummaryrefslogtreecommitdiff
path: root/test/ossfuzz/json_load_dump_fuzzer.cc
blob: 09f52d21a57c97c788c4a4fd3050d9f58c80bcad (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
#include <stdint.h>
#include <sys/types.h>

#include "jansson.h"

extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
{
  json_error_t error;

  if (size < sizeof(size_t) + sizeof(size_t))
  {
    return 0;
  }

  // Use the first sizeof(size_t) bytes as load flags.
  size_t load_flags = *(const size_t*)data;
  data += sizeof(size_t);
  size -= sizeof(size_t);

  // Use the next sizeof(size_t) bytes as dump flags.
  size_t dump_flags = *(const size_t*)data;
  data += sizeof(size_t);
  size -= sizeof(size_t);

  // Attempt to load the remainder of the data with the given load flags.
  const char* text = reinterpret_cast<const char *>(data);
  json_t* jobj = json_loadb(text, size, load_flags, &error);

  if (jobj == NULL)
  {
    return 0;
  }

  // Attempt to dump the loaded json object with the given dump flags.
  char* out = json_dumps(jobj, dump_flags);
  if (out)
  {
    free(out);
  }

  if (jobj)
  {
    json_decref(jobj);
  }

  return 0;
}