Coverage Report

Created: 2026-05-16 06:38

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