Coverage Report

Created: 2025-07-01 06:08

/src/logging-log4cxx/src/fuzzers/cpp/XMLLayoutFuzzer.cpp
Line
Count
Source
1
/*
2
 * Licensed to the Apache Software Foundation (ASF) under one or more
3
 * contributor license agreements.  See the NOTICE file distributed with
4
 * this work for additional information regarding copyright ownership.
5
 * The ASF licenses this file to You under the Apache License, Version 2.0
6
 * (the "License"); you may not use this file except in compliance with
7
 * the License.  You may obtain a copy of the License at
8
 *
9
 *      http://www.apache.org/licenses/LICENSE-2.0
10
 *
11
 * Unless required by applicable law or agreed to in writing, software
12
 * distributed under the License is distributed on an "AS IS" BASIS,
13
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14
 * See the License for the specific language governing permissions and
15
 * limitations under the License.
16
 */
17
18
#include "stdint.h"
19
#include <iostream>
20
#include <string>
21
#include <cwchar>
22
#include <log4cxx/logstring.h>
23
#include <log4cxx/ndc.h>
24
#include <log4cxx/xml/xmllayout.h>
25
#include <log4cxx/helpers/stringhelper.h>
26
#include <fuzzer/FuzzedDataProvider.h>
27
#include <log4cxx/mdc.h>
28
#include <log4cxx/helpers/transcoder.h>
29
30
using namespace log4cxx;
31
using namespace log4cxx::helpers;
32
using namespace log4cxx::spi;
33
34
1.80k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
35
  // Setup XMLLayout
36
1.80k
  log4cxx::xml::XMLLayout layout;
37
1.80k
  Pool p;
38
39
  // Create random strings
40
1.80k
  FuzzedDataProvider fdp(data, size);
41
1.80k
  std::string key1 = fdp.ConsumeRandomLengthString();
42
1.80k
  std::string val1 = fdp.ConsumeRandomLengthString();
43
1.80k
  std::string key2 = fdp.ConsumeRandomLengthString();
44
1.80k
  std::string val2 = fdp.ConsumeRandomLengthString();
45
1.80k
  std::string ndcMessage = fdp.ConsumeRandomLengthString();
46
1.80k
  std::string loggerString = fdp.ConsumeRandomLengthString();
47
1.80k
  std::string propkey = fdp.ConsumeRandomLengthString();
48
1.80k
  std::string propval = fdp.ConsumeRandomLengthString();
49
1.80k
  std::string content = fdp.ConsumeRemainingBytesAsString();
50
51
1.80k
  log4cxx::LevelPtr level = log4cxx::Level::getInfo();
52
1.80k
  log4cxx::NDC::push(ndcMessage);
53
1.80k
        LogString logstring1;
54
1.80k
        LogString key1LogString;
55
1.80k
        LogString val1LogString;
56
1.80k
        LogString propkeyLogString;
57
1.80k
        LogString propvalLogString;
58
1.80k
        LogString logger;
59
60
1.80k
        Transcoder::decode(content, logstring1);
61
1.80k
        Transcoder::decode(loggerString, logger);
62
1.80k
        Transcoder::decode(key1, key1LogString);
63
1.80k
        Transcoder::decode(val1, val1LogString);
64
1.80k
        Transcoder::decode(propkey, propkeyLogString);
65
1.80k
        Transcoder::decode(propval, propvalLogString);
66
1.80k
  log4cxx::spi::LoggingEventPtr event = log4cxx::spi::LoggingEventPtr(
67
1.80k
    new log4cxx::spi::LoggingEvent(
68
1.80k
      logger, level, logstring1, LOG4CXX_LOCATION));
69
  // Set properties
70
1.80k
  layout.setProperties(true);
71
1.80k
  event->setProperty(propkeyLogString, propvalLogString);
72
73
74
  // Set MDC
75
1.80k
  log4cxx::MDC::put(key2, val2);
76
77
  // Location info
78
1.80k
  layout.setLocationInfo(true);
79
80
  // Call the target API
81
1.80k
  log4cxx::LogString result;
82
1.80k
  layout.format(result, event, p);
83
84
  // Clean up
85
1.80k
  log4cxx::NDC::clear();
86
1.80k
  log4cxx::MDC::clear();
87
1.80k
    return 0;
88
1.80k
}