Coverage Report

Created: 2025-12-31 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/libprotobuf-mutator/examples/expat/expat_example.cc
Line
Count
Source
1
// Copyright 2017 Google Inc. All rights reserved.
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
#include <vector>
16
17
#include "expat.h"  // NOLINT
18
19
#include "examples/xml/xml.pb.h"
20
#include "examples/xml/xml_writer.h"
21
#include "src/libfuzzer/libfuzzer_macro.h"
22
23
namespace {
24
const std::vector<const char*> kEncodings = {{"UTF-16", "UTF-8", "ISO-8859-1",
25
                                              "US-ASCII", "UTF-16BE",
26
                                              "UTF-16LE", "INVALIDENCODING"}};
27
}
28
29
11.4k
DEFINE_PROTO_FUZZER(const protobuf_mutator::xml::Input& message) {
30
11.4k
  std::string xml = MessageToXml(message.document());
31
11.4k
  int options = message.options();
32
33
11.4k
  int use_ns = options % 2;
34
11.4k
  options /= 2;
35
11.4k
  auto enc = kEncodings[options % kEncodings.size()];
36
11.4k
  XML_Parser parser =
37
11.4k
      use_ns ? XML_ParserCreateNS(enc, '\n') : XML_ParserCreate(enc);
38
11.4k
  XML_Parse(parser, xml.data(), xml.size(), true);
39
11.4k
  XML_ParserFree(parser);
40
11.4k
}