mirror of
https://gitlab.gnome.org/GNOME/libxml2.git
synced 2026-01-26 21:41:34 +03:00
80 lines
2.0 KiB
C
80 lines
2.0 KiB
C
/*
|
|
* schema.c: a libFuzzer target to test the XML Schema processor.
|
|
*
|
|
* See Copyright for the status of this software.
|
|
*/
|
|
|
|
#ifndef XML_DEPRECATED
|
|
#define XML_DEPRECATED
|
|
#endif
|
|
|
|
#include <libxml/catalog.h>
|
|
#include <libxml/xmlschemas.h>
|
|
#include <libxml/xmlschemastypes.h>
|
|
#include "fuzz.h"
|
|
|
|
int
|
|
LLVMFuzzerInitialize(int *argc ATTRIBUTE_UNUSED,
|
|
char ***argv ATTRIBUTE_UNUSED) {
|
|
xmlFuzzMemSetup();
|
|
xmlInitParser();
|
|
#ifdef LIBXML_CATALOG_ENABLED
|
|
xmlInitializeCatalog();
|
|
xmlCatalogSetDefaults(XML_CATA_ALLOW_NONE);
|
|
#endif
|
|
|
|
return 0;
|
|
}
|
|
|
|
int
|
|
LLVMFuzzerTestOneInput(const char *data, size_t size) {
|
|
xmlSchemaParserCtxtPtr pctxt;
|
|
xmlSchemaPtr schema;
|
|
size_t failurePos;
|
|
|
|
if (size > 200000)
|
|
return(0);
|
|
|
|
xmlFuzzDataInit(data, size);
|
|
|
|
failurePos = xmlFuzzReadInt(4) % (size + 100);
|
|
|
|
xmlFuzzReadEntities();
|
|
|
|
xmlFuzzInjectFailure(failurePos);
|
|
pctxt = xmlSchemaNewParserCtxt(xmlFuzzMainUrl());
|
|
xmlSchemaSetParserStructuredErrors(pctxt, xmlFuzzSErrorFunc, NULL);
|
|
xmlSchemaSetResourceLoader(pctxt, xmlFuzzResourceLoader, NULL);
|
|
schema = xmlSchemaParse(pctxt);
|
|
xmlSchemaFreeParserCtxt(pctxt);
|
|
|
|
if (schema != NULL) {
|
|
xmlSchemaValidCtxtPtr vctxt;
|
|
xmlParserCtxtPtr ctxt;
|
|
xmlDocPtr doc;
|
|
|
|
ctxt = xmlNewParserCtxt();
|
|
xmlCtxtSetErrorHandler(ctxt, xmlFuzzSErrorFunc, NULL);
|
|
xmlCtxtSetResourceLoader(ctxt, xmlFuzzResourceLoader, NULL);
|
|
doc = xmlCtxtReadFile(ctxt, xmlFuzzSecondaryUrl(), NULL,
|
|
XML_PARSE_NOENT);
|
|
xmlFreeParserCtxt(ctxt);
|
|
|
|
vctxt = xmlSchemaNewValidCtxt(schema);
|
|
xmlSchemaSetValidStructuredErrors(vctxt, xmlFuzzSErrorFunc, NULL);
|
|
xmlSchemaValidateDoc(vctxt, doc);
|
|
xmlSchemaFreeValidCtxt(vctxt);
|
|
|
|
xmlFreeDoc(doc);
|
|
xmlSchemaFree(schema);
|
|
}
|
|
|
|
xmlFuzzInjectFailure(0);
|
|
xmlFuzzDataCleanup();
|
|
xmlResetLastError();
|
|
xmlSchemaCleanupTypes();
|
|
|
|
return(0);
|
|
}
|
|
|