Coverage Report

Created: 2026-03-07 06:49

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