Coverage Report

Created: 2023-06-07 06:33

/src/uri_dissect_query_malloc_fuzzer.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2020 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
// Fuzz UriQuery.c:
16
//   uriDissectQueryMallocA
17
//   uriComposeQueryA
18
19
#include <cstddef>
20
#include <cstdint>
21
#include <string>
22
#include <utility>
23
#include <vector>
24
25
using std::string;
26
#include "uriparser/include/uriparser/Uri.h"
27
28
1.50k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
29
30
1.50k
  const string query(reinterpret_cast<const char *>(data), size);
31
32
1.50k
  UriQueryListA *query_list = nullptr;
33
1.50k
  int item_count = -1;
34
35
1.50k
  const char *query_start = query.c_str();
36
1.50k
  const char *query_end = query_start + size;
37
38
  // Break a query like "a=b&2=3" into key/value pairs.
39
1.50k
  int result =
40
1.50k
      uriDissectQueryMallocA(&query_list, &item_count, query_start, query_end);
41
42
1.50k
  if (query_list == nullptr || result != URI_SUCCESS || item_count < 0)
43
8
    return 0;
44
45
1.49k
  int chars_required = 0;
46
1.49k
  if (uriComposeQueryCharsRequiredA(query_list, &chars_required) != URI_SUCCESS)
47
0
    return 0;
48
49
1.49k
  if (!chars_required) {
50
16
    uriFreeQueryListA(query_list);
51
16
    return 0;
52
16
  }
53
54
1.47k
  std::vector<char> buf(chars_required, 0);
55
1.47k
  int written = -1;
56
  // Reverse the process of uriDissectQueryMallocA.
57
1.47k
  result = uriComposeQueryA(buf.data(), query_list, chars_required, &written);
58
59
1.47k
  uriFreeQueryListA(query_list);
60
1.47k
  return 0;
61
1.49k
}