Coverage Report

Created: 2025-07-11 07:00

/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
2.98k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size) {
32
  // Setup HTMLLayout
33
2.98k
  HTMLLayout layout;
34
2.98k
  Pool p;
35
36
2.98k
  FuzzedDataProvider fdp(data, size);
37
38
  // Optional locationinfo
39
2.98k
  if (fdp.ConsumeBool()) {
40
1.71k
    layout.setOption(LOG4CXX_STR("LOCATIONINFO"), LOG4CXX_STR("true"));
41
1.71k
  }
42
  // Optional threadinfo
43
2.98k
  if (fdp.ConsumeBool()) {
44
1.04k
    LOG4CXX_DECODE_CHAR(title, fdp.ConsumeRandomLengthString());
45
1.04k
    layout.setOption(LOG4CXX_STR("TITLE"), title);
46
1.04k
  }
47
48
  // Header
49
2.98k
  if (fdp.ConsumeBool()) {
50
1.06k
    std::string headerStr = fdp.ConsumeRandomLengthString();
51
1.06k
    LogString header;
52
1.06k
    Transcoder::decode(headerStr, header);
53
1.06k
    layout.appendHeader(header, p);
54
1.06k
  }
55
56
  // Create random strings we need later
57
2.98k
  std::string key1Str = fdp.ConsumeRandomLengthString();
58
2.98k
  std::string val1Str = fdp.ConsumeRandomLengthString();
59
2.98k
  std::string key2Str = fdp.ConsumeRandomLengthString();
60
2.98k
  std::string val2Str = fdp.ConsumeRandomLengthString();
61
2.98k
  std::string key3 = fdp.ConsumeRandomLengthString();
62
2.98k
  std::string val3 = fdp.ConsumeRandomLengthString();
63
2.98k
  std::string key4 = fdp.ConsumeRandomLengthString();
64
2.98k
  std::string val4 = fdp.ConsumeRandomLengthString();
65
2.98k
  std::string ndcMessage = fdp.ConsumeRandomLengthString();
66
2.98k
  std::string loggerStr = fdp.ConsumeRandomLengthString();
67
2.98k
  std::string contentStr = fdp.ConsumeRemainingBytesAsString();
68
69
2.98k
  LogString key1, key2, val1, val2, logger, content;
70
71
2.98k
  Transcoder::decode(loggerStr, logger);
72
2.98k
  Transcoder::decode(key1Str, key1);
73
2.98k
  Transcoder::decode(val1Str, val2);
74
2.98k
  Transcoder::decode(key2Str, key2);
75
2.98k
  Transcoder::decode(val2Str, val2);
76
2.98k
  Transcoder::decode(contentStr, content);
77
  
78
2.98k
  LevelPtr level = log4cxx::Level::getInfo();
79
2.98k
  NDC::push(ndcMessage);
80
2.98k
  spi::LoggingEventPtr event = log4cxx::spi::LoggingEventPtr(
81
2.98k
    new log4cxx::spi::LoggingEvent(
82
2.98k
      logger, level, content, LOG4CXX_LOCATION));
83
84
  // Set properties
85
2.98k
  event->setProperty(key1, val1);
86
2.98k
  event->setProperty(key2, val2);
87
88
  // Set MDC
89
2.98k
  log4cxx::MDC::put(key3, val3);
90
2.98k
  log4cxx::MDC::put(key4, val4);
91
92
  // Call the target API
93
2.98k
  log4cxx::LogString result;
94
2.98k
  layout.format(result, event, p);
95
96
  // Clean up
97
2.98k
  log4cxx::NDC::clear();
98
2.98k
  log4cxx::MDC::clear();
99
2.98k
    return 0;
100
2.98k
}