Coverage Report

Created: 2026-08-31 07:52

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
13.1k
{
12
13.1k
  if (tmpName.empty())
13
2
    tmpName = tmpnam(nullptr);
14
15
13.1k
  if (tmpFile.empty())
16
2
    tmpFile = tmpName + FILE_EXT;
17
18
13.1k
  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
13.1k
  pcpp::Logger::getInstance().suppressLogs();
25
26
13.1k
  std::unique_ptr<pcpp::IFileReaderDevice> reader(pcpp::IFileReaderDevice::getReader(tmpFile));
27
13.1k
  if (!reader->open())
28
259
  {
29
259
    std::cerr << "Error opening the '" << tmpFile << "' file\n";
30
259
    return -1;
31
259
  }
32
33
12.8k
  pcpp::PcapStats stats;
34
12.8k
  reader->getStatistics(stats);
35
12.8k
  std::cout << "Read " << stats.packetsRecv << " packets successfully and " << stats.packetsDrop
36
12.8k
            << " packets could not be read" << std::endl;
37
38
12.8k
  if (auto ngReader = dynamic_cast<pcpp::PcapNgFileReaderDevice*>(reader.get()))
39
12.7k
  {
40
12.7k
    std::cout << "OS is '" << ngReader->getOS() << "'; Hardware is '" << ngReader->getHardware() << "'"
41
12.7k
              << "'; CaptureApplication is '" << ngReader->getCaptureApplication() << "'; CaptureFileComment is '"
42
12.7k
              << ngReader->getCaptureFileComment() << "'" << std::endl;
43
12.7k
  }
44
45
12.8k
  pcpp::RawPacketVector packets;
46
12.8k
  if (reader->getNextPackets(packets, 1) != 1)
47
670
  {
48
670
    std::cerr << "Couldn't read the first packet in the file\n";
49
670
    return 0;
50
670
  }
51
52
12.2k
  pcpp::RawPacket& rawPacket = *packets.front();
53
12.2k
  do
54
202k
  {
55
    // go deeper only for .pcap and .pcapng format
56
    // for .snoop we are only fuzzing the reader
57
202k
    if (0 == strcmp(FILE_EXT, ".pcap") || 0 == strcmp(FILE_EXT, ".pcapng"))
58
201k
    {
59
201k
      pcpp::Packet parsedPacket(&rawPacket);
60
201k
      parsedPacket.toString();
61
201k
      auto layer = parsedPacket.getFirstLayer();
62
1.10M
      while (layer != nullptr)
63
906k
      {
64
906k
        std::cout << layer->toString() << std::endl;
65
906k
        layer->getHeaderLen();
66
906k
        readParsedPacket(parsedPacket, layer);
67
906k
        layer = layer->getNextLayer();
68
906k
      }
69
201k
      parsedPacket.computeCalculateFields();
70
201k
    }
71
202k
  } while (reader->getNextPacket(rawPacket));
72
73
12.2k
  reader->close();
74
12.2k
  return 0;
75
12.8k
}