Coverage Report

Created: 2024-07-27 07:03

/src/format_fuzzer.cc
Line
Count
Source (jump to first uncovered line)
1
// Copyright 2020 Google LLC
2
//
3
// Licensed under the Apache License, Version 2.0 (the "License");
4
// you may not use this file except in compliance with the License.
5
// You may obtain a copy of the License at
6
//
7
//      http://www.apache.org/licenses/LICENSE-2.0
8
//
9
// Unless required by applicable law or agreed to in writing, software
10
// distributed under the License is distributed on an "AS IS" BASIS,
11
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12
// See the License for the specific language governing permissions and
13
// limitations under the License.
14
15
#include <cstddef>
16
17
#include <fuzzer/FuzzedDataProvider.h>
18
19
#include "spdlog/spdlog.h"
20
#include "spdlog/sinks/basic_file_sink.h"
21
#include "spdlog/pattern_formatter.h"
22
23
24
std::string my_formatter_txt = "custom-flag";
25
26
class my_formatter_flag : public spdlog::custom_flag_formatter
27
{
28
  
29
public:
30
    void format(const spdlog::details::log_msg &, const std::tm &, spdlog::memory_buf_t &dest) override
31
5.41k
    {
32
5.41k
        dest.append(my_formatter_txt.data(), my_formatter_txt.data() + my_formatter_txt.size());
33
5.41k
    }
34
35
    std::unique_ptr<custom_flag_formatter> clone() const override
36
33.4k
    {
37
33.4k
        return spdlog::details::make_unique<my_formatter_flag>();
38
33.4k
    }
39
};
40
41
8.22k
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size) {
42
8.22k
  if (size == 0) {
43
0
    return 0;
44
0
  }
45
46
8.22k
  static std::shared_ptr<spdlog::logger> my_logger;
47
8.22k
  if (!my_logger.get()) {
48
1
    my_logger = spdlog::basic_logger_mt("basic_logger", "/dev/null");
49
1
    spdlog::set_default_logger(my_logger);
50
1
  }
51
52
8.22k
  FuzzedDataProvider stream(data, size);
53
54
8.22k
  const unsigned long size_arg = stream.ConsumeIntegral<unsigned long>();
55
8.22k
  const unsigned long  int_arg = stream.ConsumeIntegral<unsigned long>();
56
8.22k
  const char flag = (char)(stream.ConsumeIntegral<char>());
57
8.22k
  const std::string pattern = stream.ConsumeRandomLengthString();
58
8.22k
  my_formatter_txt = stream.ConsumeRandomLengthString();
59
8.22k
  const std::string string_arg = stream.ConsumeRandomLengthString();
60
8.22k
  const std::string format_string = stream.ConsumeRemainingBytesAsString();
61
62
8.22k
  using spdlog::details::make_unique;
63
8.22k
  auto formatter = make_unique<spdlog::pattern_formatter>();
64
8.22k
  formatter->add_flag<my_formatter_flag>(flag).set_pattern(pattern);
65
8.22k
  spdlog::set_formatter(std::move(formatter));
66
  
67
8.22k
  spdlog::info(format_string.c_str(), size_arg, int_arg, string_arg);
68
69
8.22k
  return 0;
70
8.22k
}