/src/libprotobuf-mutator/examples/libxml2/libxml2_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 "libxml/parser.h" |
16 | | #include "libxml/xmlsave.h" |
17 | | |
18 | | #include "examples/xml/xml.pb.h" |
19 | | #include "examples/xml/xml_writer.h" |
20 | | #include "src/libfuzzer/libfuzzer_macro.h" |
21 | | |
22 | | namespace { |
23 | 2.77M | void ignore(void* ctx, const char* msg, ...) {} |
24 | | |
25 | | template <class T, class D> |
26 | 40.1k | std::unique_ptr<T, D> MakeUnique(T* obj, D del) { |
27 | 40.1k | return {obj, del}; |
28 | 40.1k | } libxml2_example.cc:std::__1::unique_ptr<_xmlDoc, void (*)(_xmlDoc*)> (anonymous namespace)::MakeUnique<_xmlDoc, void (*)(_xmlDoc*)>(_xmlDoc*, void (*)(_xmlDoc*)) Line | Count | Source | 26 | 21.8k | std::unique_ptr<T, D> MakeUnique(T* obj, D del) { | 27 | 21.8k | return {obj, del}; | 28 | 21.8k | } |
libxml2_example.cc:std::__1::unique_ptr<_xmlBuffer, void (*)(_xmlBuffer*)> (anonymous namespace)::MakeUnique<_xmlBuffer, void (*)(_xmlBuffer*)>(_xmlBuffer*, void (*)(_xmlBuffer*)) Line | Count | Source | 26 | 9.18k | std::unique_ptr<T, D> MakeUnique(T* obj, D del) { | 27 | 9.18k | return {obj, del}; | 28 | 9.18k | } |
libxml2_example.cc:std::__1::unique_ptr<_xmlSaveCtxt, int (*)(_xmlSaveCtxt*)> (anonymous namespace)::MakeUnique<_xmlSaveCtxt, int (*)(_xmlSaveCtxt*)>(_xmlSaveCtxt*, int (*)(_xmlSaveCtxt*)) Line | Count | Source | 26 | 9.18k | std::unique_ptr<T, D> MakeUnique(T* obj, D del) { | 27 | 9.18k | return {obj, del}; | 28 | 9.18k | } |
|
29 | | } // namespace |
30 | | |
31 | 21.8k | DEFINE_PROTO_FUZZER(const protobuf_mutator::xml::Input& message) { |
32 | 21.8k | std::string xml = MessageToXml(message.document()); |
33 | 21.8k | int options = message.options(); |
34 | | |
35 | | // Network requests are too slow. |
36 | 21.8k | options |= XML_PARSE_NONET; |
37 | | // These flags can cause network or file access and hangs. |
38 | 21.8k | options &= ~(XML_PARSE_NOENT | XML_PARSE_HUGE | XML_PARSE_DTDVALID | |
39 | 21.8k | XML_PARSE_DTDLOAD | XML_PARSE_DTDATTR); |
40 | | |
41 | 21.8k | xmlSetGenericErrorFunc(nullptr, &ignore); |
42 | | |
43 | 21.8k | if (auto doc = |
44 | 21.8k | MakeUnique(xmlReadMemory(xml.c_str(), static_cast<int>(xml.size()), |
45 | 21.8k | "", nullptr, options), |
46 | 21.8k | &xmlFreeDoc)) { |
47 | 9.18k | auto buf = MakeUnique(xmlBufferCreate(), &xmlBufferFree); |
48 | 9.18k | auto ctxt = |
49 | 9.18k | MakeUnique(xmlSaveToBuffer(buf.get(), nullptr, 0), &xmlSaveClose); |
50 | 9.18k | xmlSaveDoc(ctxt.get(), doc.get()); |
51 | 9.18k | } |
52 | 21.8k | } |