/ libxml2 / fuzz / libxml2_xml_regexp_compile_fuzzer.cc
libxml2_xml_regexp_compile_fuzzer.cc
 1  // Copyright 2016 The Chromium Authors. All rights reserved.
 2  // Use of this source code is governed by a BSD-style license that can be
 3  // found in the LICENSE file.
 4  
 5  #include <stddef.h>
 6  #include <stdint.h>
 7  
 8  #include <algorithm>
 9  #include <string>
10  #include <vector>
11  
12  #include "libxml/parser.h"
13  #include "libxml/tree.h"
14  #include "libxml/xmlversion.h"
15  
16  extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size);
17  
18  static void ignore (void * ctx, const char * msg, ...) {
19    // Error handler to avoid spam of error messages from libxml parser.
20  }
21  
22  
23  // Entry point for LibFuzzer.
24  extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
25    xmlSetGenericErrorFunc(NULL, &ignore);
26  
27    std::vector<uint8_t> buffer(size + 1, 0);
28    std::copy(data, data + size, buffer.data());
29  
30    xmlRegexpPtr x = xmlRegexpCompile(buffer.data());
31    if (x)
32      xmlRegFreeRegexp(x);
33  
34    return 0;
35  }