Coverage Report

Created: 2026-07-16 07:15

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