Coverage Report

Created: 2022-03-15 07:22

/src/convert_jsonnet_fuzzer.cc
Line
Count
Source
1
#include <cstddef>
2
#include <cstdint>
3
#include <cstring>
4
#include <string>
5
6
extern "C" {
7
#include "libjsonnet.h"
8
}
9
10
char* ImportCallback(void* ctx, const char* base, const char* rel,
11
20
                     char** found_here, int* success) {
12
  // Don't load file and mark it as failure.
13
20
  *success = 0;
14
20
  char* res = jsonnet_realloc(static_cast<struct JsonnetVm*>(ctx), nullptr, 1);
15
20
  res[0] = 0;
16
20
  return res;
17
20
}
18
19
434
std::string ConvertJsonnetToJson(const std::string& jsonnet) {
20
434
  JsonnetVm* jvm = jsonnet_make();
21
434
  jsonnet_import_callback(jvm, ImportCallback, jvm);
22
434
  int error = 0;
23
434
  char* res =
24
434
      jsonnet_evaluate_snippet(jvm, /*filename=*/"", jsonnet.c_str(), &error);
25
26
434
  std::string json;
27
434
  if (error == 0 && res != nullptr) {
28
133
    json = res;
29
133
  }
30
31
434
  if (res) {
32
434
    jsonnet_realloc(jvm, res, 0);
33
434
  }
34
434
  jsonnet_destroy(jvm);
35
434
  return json;
36
434
}
37
38
434
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
39
434
  std::string fuzz_jsonnet(reinterpret_cast<const char*>(data), size);
40
434
  ConvertJsonnetToJson(fuzz_jsonnet);
41
434
  return 0;
42
434
}