Coverage Report

Created: 2026-09-14 06:53

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/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
namespace
34
{
35
  const int MaxKeyLength = 50;
36
  const int MaxValueLength = 500;
37
  const int MaxMessageLength = 2000;
38
}
39
40
0
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
41
  // Setup XMLLayout
42
0
  log4cxx::xml::XMLLayout layout;
43
44
  // Create random strings
45
0
  FuzzedDataProvider fdp(data, size);
46
0
  std::string key1 = fdp.ConsumeRandomLengthString(MaxKeyLength);
47
0
  std::string val1 = fdp.ConsumeRandomLengthString(MaxValueLength);
48
0
  std::string key2 = fdp.ConsumeRandomLengthString(MaxKeyLength);
49
0
  std::string val2 = fdp.ConsumeRandomLengthString(MaxValueLength);
50
0
  std::string ndcMessage = fdp.ConsumeRandomLengthString(MaxMessageLength);
51
0
  std::string loggerString = fdp.ConsumeRandomLengthString(MaxKeyLength);
52
0
  std::string propkey = fdp.ConsumeRandomLengthString(MaxKeyLength);
53
0
  std::string propval = fdp.ConsumeRandomLengthString(MaxValueLength);
54
0
  std::string content = fdp.ConsumeRemainingBytesAsString();
55
56
0
  log4cxx::LevelPtr level = log4cxx::Level::getInfo();
57
0
  log4cxx::NDC::push(ndcMessage);
58
0
        LogString logstring1;
59
0
        LogString key1LogString;
60
0
        LogString val1LogString;
61
0
        LogString propkeyLogString;
62
0
        LogString propvalLogString;
63
0
        LogString logger;
64
65
0
        Transcoder::decode(content, logstring1);
66
0
        Transcoder::decode(loggerString, logger);
67
0
        Transcoder::decode(key1, key1LogString);
68
0
        Transcoder::decode(val1, val1LogString);
69
0
        Transcoder::decode(propkey, propkeyLogString);
70
0
        Transcoder::decode(propval, propvalLogString);
71
0
  log4cxx::spi::LoggingEventPtr event = log4cxx::spi::LoggingEventPtr(
72
0
    new log4cxx::spi::LoggingEvent(
73
0
      logger, level, logstring1, LOG4CXX_LOCATION));
74
  // Set properties
75
0
  layout.setProperties(true);
76
0
  event->setProperty(propkeyLogString, propvalLogString);
77
78
79
  // Set MDC
80
0
  log4cxx::MDC::put(key2, val2);
81
82
  // Location info
83
0
  layout.setLocationInfo(true);
84
85
  // Call the target API
86
0
  log4cxx::LogString result;
87
0
  layout.format(result, event);
88
89
  // Clean up
90
0
  log4cxx::NDC::clear();
91
0
  log4cxx::MDC::clear();
92
0
    return 0;
93
0
}