Avoid potential negative array index access to cached text.
[LibreOffice.git] / vcl / workben / fodt2pdffuzzer.cxx
blob0b6323a61ad8b0994a3732a509b22e3891730b8a
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
2 /*
3 * This file is part of the LibreOffice project.
5 * This Source Code Form is subject to the terms of the Mozilla Public
6 * License, v. 2.0. If a copy of the MPL was not distributed with this
7 * file, You can obtain one at http://mozilla.org/MPL/2.0/.
8 */
10 #include <tools/stream.hxx>
11 #include <vcl/FilterConfigItem.hxx>
12 #include <com/sun/star/awt/XToolkit.hpp>
13 #include <com/sun/star/ucb/XContentProvider.hpp>
14 #include <com/sun/star/ucb/XUniversalContentBroker.hpp>
15 #include <libxml/parser.h>
16 #include "commonfuzzer.hxx"
18 extern "C" void* SwCreateDialogFactory() { return nullptr; }
20 extern "C" bool TestPDFExportFODT(SvStream& rStream);
22 static void silent_error_func(void*, const char* /*format*/, ...) {}
24 extern "C" int LLVMFuzzerInitialize(int* argc, char*** argv)
26 if (__lsan_disable)
27 __lsan_disable();
29 CommonInitialize(argc, argv);
31 // initialise unconfigured UCB:
32 css::uno::Reference<css::ucb::XUniversalContentBroker> xUcb(
33 comphelper::getProcessServiceFactory()->createInstance(
34 "com.sun.star.ucb.UniversalContentBroker"),
35 css::uno::UNO_QUERY_THROW);
36 css::uno::Sequence<css::uno::Any> aArgs{ css::uno::Any(OUString("NoConfig")) };
37 css::uno::Reference<css::ucb::XContentProvider> xFileProvider(
38 comphelper::getProcessServiceFactory()->createInstanceWithArguments(
39 "com.sun.star.ucb.FileContentProvider", aArgs),
40 css::uno::UNO_QUERY_THROW);
41 xUcb->registerContentProvider(xFileProvider, "file", true);
43 // create and hold a reference to XToolkit here to avoid the lsan warning about its leak
44 // due to getting created in the unusual case of no vcl main loop
45 static css::uno::Reference<css::awt::XToolkit> xTk(
46 comphelper::getProcessServiceFactory()->createInstance("com.sun.star.awt.Toolkit"),
47 css::uno::UNO_QUERY_THROW);
49 if (__lsan_enable)
50 __lsan_enable();
52 xmlInitParser();
53 xmlSetGenericErrorFunc(nullptr, silent_error_func);
55 return 0;
58 extern "C" size_t LLVMFuzzerMutate(uint8_t* Data, size_t Size, size_t MaxSize);
60 extern "C" size_t LLVMFuzzerCustomMutator(uint8_t* Data, size_t Size, size_t MaxSize,
61 unsigned int /*Seed*/)
63 size_t Ret = LLVMFuzzerMutate(Data, Size, MaxSize);
65 // an effort to only generate valid xml, in this fuzzer we only really care
66 // about the deeper levels of turning valid input into writer layout and
67 // pdf export
69 xmlParserCtxtPtr ctxt = xmlNewParserCtxt();
70 xmlDocPtr Doc = xmlCtxtReadMemory(ctxt, reinterpret_cast<const char*>(Data), Ret, nullptr,
71 nullptr, XML_PARSE_NONET);
72 if (Doc == nullptr)
73 Ret = 0;
74 else
75 xmlFreeDoc(Doc);
76 xmlFreeParserCtxt(ctxt);
77 xmlResetLastError();
78 return Ret;
81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
83 SvMemoryStream aStream(const_cast<uint8_t*>(data), size, StreamMode::READ);
84 bool bFODTLoaded = TestPDFExportFODT(aStream);
85 // if the fodt didn't load then reject so that input will not be added to the corpus
86 // we're not interested in input that doesn't go on to exercise the pdf export
87 return bFODTLoaded ? 0 : -1;
90 /* vim:set shiftwidth=4 softtabstop=4 expandtab: */