Coverage Report

Created: 2025-11-11 06:59

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/logging-log4cxx/src/fuzzers/cpp/HTMLLayoutFuzzer.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 <log4cxx/logstring.h>
20
#include <log4cxx/ndc.h>
21
#include <log4cxx/helpers/stringhelper.h>
22
#include <fuzzer/FuzzedDataProvider.h>
23
#include <log4cxx/mdc.h>
24
#include <log4cxx/htmllayout.h>
25
#include <log4cxx/helpers/transcoder.h>
26
27
using namespace log4cxx;
28
using namespace log4cxx::helpers;
29
using namespace log4cxx::spi;
30
31
3.71k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
32
  // Setup HTMLLayout
33
3.71k
  HTMLLayout layout;
34
3.71k
  Pool p;
35
36
3.71k
  FuzzedDataProvider fdp(data, size);
37
38
  // Optional locationinfo
39
3.71k
  if (fdp.ConsumeBool()) {
40
2.01k
    layout.setOption(LOG4CXX_STR("LOCATIONINFO"), LOG4CXX_STR("true"));
41
2.01k
  }
42
  // Optional threadinfo
43
3.71k
  if (fdp.ConsumeBool()) {
44
1.28k
    LOG4CXX_DECODE_CHAR(title, fdp.ConsumeRandomLengthString());
45
1.28k
    layout.setOption(LOG4CXX_STR("TITLE"), title);
46
1.28k
  }
47
48
  // Header
49
3.71k
  if (fdp.ConsumeBool()) {
50
1.46k
    std::string headerStr = fdp.ConsumeRandomLengthString();
51
1.46k
    LogString header;
52
1.46k
    Transcoder::decode(headerStr, header);
53
1.46k
    layout.appendHeader(header, p);
54
1.46k
  }
55
56
  // Create random strings we need later
57
3.71k
  std::string key1Str = fdp.ConsumeRandomLengthString();
58
3.71k
  std::string val1Str = fdp.ConsumeRandomLengthString();
59
3.71k
  std::string key2Str = fdp.ConsumeRandomLengthString();
60
3.71k
  std::string val2Str = fdp.ConsumeRandomLengthString();
61
3.71k
  std::string key3 = fdp.ConsumeRandomLengthString();
62
3.71k
  std::string val3 = fdp.ConsumeRandomLengthString();
63
3.71k
  std::string key4 = fdp.ConsumeRandomLengthString();
64
3.71k
  std::string val4 = fdp.ConsumeRandomLengthString();
65
3.71k
  std::string ndcMessage = fdp.ConsumeRandomLengthString();
66
3.71k
  std::string loggerStr = fdp.ConsumeRandomLengthString();
67
3.71k
  std::string contentStr = fdp.ConsumeRemainingBytesAsString();
68
69
3.71k
  LogString key1, key2, val1, val2, logger, content;
70
71
3.71k
  Transcoder::decode(loggerStr, logger);
72
3.71k
  Transcoder::decode(key1Str, key1);
73
3.71k
  Transcoder::decode(val1Str, val2);
74
3.71k
  Transcoder::decode(key2Str, key2);
75
3.71k
  Transcoder::decode(val2Str, val2);
76
3.71k
  Transcoder::decode(contentStr, content);
77
  
78
3.71k
  LevelPtr level = log4cxx::Level::getInfo();
79
3.71k
  NDC::push(ndcMessage);
80
3.71k
  spi::LoggingEventPtr event = log4cxx::spi::LoggingEventPtr(
81
3.71k
    new log4cxx::spi::LoggingEvent(
82
3.71k
      logger, level, content, LOG4CXX_LOCATION));
83
84
  // Set properties
85
3.71k
  event->setProperty(key1, val1);
86
3.71k
  event->setProperty(key2, val2);
87
88
  // Set MDC
89
3.71k
  log4cxx::MDC::put(key3, val3);
90
3.71k
  log4cxx::MDC::put(key4, val4);
91
92
  // Call the target API
93
3.71k
  log4cxx::LogString result;
94
3.71k
  layout.format(result, event, p);
95
96
  // Clean up
97
3.71k
  log4cxx::NDC::clear();
98
3.71k
  log4cxx::MDC::clear();
99
3.71k
    return 0;
100
3.71k
}