Skip to content
Myr

jq_fuzz_execute.cpp

This file

Path

tests/jq_fuzz_execute.cpp

48 lines · 1.3 KiB · C++

Last changed by

jq_fuzz_execute: limit oniguruma depth to 1024 (#3377)Sudhakar Vermabefore adoption589a694

History of this file

Take it

1#include <fuzzer/FuzzedDataProvider.h>2#include <string>34#include "jq.h"5#include "jv.h"6#include "oniguruma.h"78extern "C" int LLVMFuzzerInitialize(int *argc, char ***argv) {9  onig_set_parse_depth_limit(1024);10  return 0;11}1213// Fuzzer inspired by /src/jq_test.c14// The goal is to have the fuzzer execute the functions:15// jq_compile -> jv_parse -> jq_next.16extern "C" int LLVMFuzzerTestOneInput(uint8_t *data, size_t size) {17  FuzzedDataProvider fdp(data, size);18  std::string prog_payload = fdp.ConsumeRandomLengthString();19  std::string parse_payload1 = fdp.ConsumeRandomLengthString();20  std::string parse_payload2 = fdp.ConsumeRandomLengthString();2122  jq_state *jq = NULL;23  jq = jq_init();24  if (jq != NULL) {25    jq_set_attr(jq, jv_string("JQ_ORIGIN"), jv_string("/tmp/"));2627    if (jq_compile(jq, prog_payload.c_str())) {28      // Process to jv_parse and then jv_next29      jv input = jv_parse(parse_payload1.c_str());30      if (jv_is_valid(input)) {31        jq_start(jq, input, 0);32        jv next = jv_parse(parse_payload2.c_str());33        if (jv_is_valid(next)) {34          jv actual = jq_next(jq);35          jv_free(actual);36        }37        jv_free(next);38      } else {39        // Only free if input is invalid as otherwise jq_teardown40        // frees it.41        jv_free(input);42      }43    }44  }45  jq_teardown(&jq);4647  return 0;48}