Coverage Report

Created: 2026-02-14 06:19

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