Coverage Report

Created: 2026-02-26 06:41

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PcapPlusPlus/Tests/Fuzzers/FuzzTarget.cpp
Line
Count
Source
1
#include <PcapFileDevice.h>
2
#include <Packet.h>
3
#include <Logger.h>
4
#include "DumpToFile.h"
5
#include "ReadParsedPacket.h"
6
7
static std::string tmpName;
8
static std::string tmpFile;
9
10
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
11
15.0k
{
12
15.0k
  if (tmpName.empty())
13
1
    tmpName = tmpnam(nullptr);
14
15
15.0k
  if (tmpFile.empty())
16
1
    tmpFile = tmpName + FILE_EXT;
17
18
15.0k
  if (dumpDataToPcapFile(data, size, tmpFile.c_str()) != 0)
19
0
  {
20
0
    std::cerr << "Can't Dump buffer to the '" << tmpFile << "' file!!!!\n";
21
0
    return -1;
22
0
  }
23
24
15.0k
  pcpp::Logger::getInstance().suppressLogs();
25
26
15.0k
  std::unique_ptr<pcpp::IFileReaderDevice> reader(pcpp::IFileReaderDevice::getReader(tmpFile));
27
15.0k
  if (!reader->open())
28
397
  {
29
397
    std::cerr << "Error opening the '" << tmpFile << "' file\n";
30
397
    return -1;
31
397
  }
32
33
14.6k
  pcpp::PcapStats stats;
34
14.6k
  reader->getStatistics(stats);
35
14.6k
  std::cout << "Read " << stats.packetsRecv << " packets successfully and " << stats.packetsDrop
36
14.6k
            << " packets could not be read" << std::endl;
37
38
14.6k
  if (auto ngReader = dynamic_cast<pcpp::PcapNgFileReaderDevice*>(reader.get()))
39
0
  {
40
0
    std::cout << "OS is '" << ngReader->getOS() << "'; Hardware is '" << ngReader->getHardware() << "'"
41
0
              << "'; CaptureApplication is '" << ngReader->getCaptureApplication() << "'; CaptureFileComment is '"
42
0
              << ngReader->getCaptureFileComment() << "'" << std::endl;
43
0
  }
44
45
14.6k
  pcpp::RawPacketVector packets;
46
14.6k
  if (reader->getNextPackets(packets, 1) != 1)
47
353
  {
48
353
    std::cerr << "Couldn't read the first packet in the file\n";
49
353
    return 0;
50
353
  }
51
52
14.2k
  pcpp::RawPacket& rawPacket = *packets.front();
53
14.2k
  do
54
257k
  {
55
    // go deeper only for .pcap and .pcapng format
56
    // for .snoop we are only fuzzing the reader
57
257k
    if (0 == strcmp(FILE_EXT, ".pcap") || 0 == strcmp(FILE_EXT, ".pcapng"))
58
257k
    {
59
257k
      pcpp::Packet parsedPacket(&rawPacket);
60
257k
      parsedPacket.toString();
61
257k
      auto layer = parsedPacket.getFirstLayer();
62
1.31M
      while (layer != nullptr)
63
1.06M
      {
64
1.06M
        std::cout << layer->toString() << std::endl;
65
1.06M
        layer->getHeaderLen();
66
1.06M
        readParsedPacket(parsedPacket, layer);
67
1.06M
        layer = layer->getNextLayer();
68
1.06M
      }
69
257k
      parsedPacket.computeCalculateFields();
70
257k
    }
71
257k
  } while (reader->getNextPacket(rawPacket));
72
73
14.2k
  reader->close();
74
14.2k
  return 0;
75
14.6k
}