Coverage Report

Created: 2026-06-30 07:04

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PcapPlusPlus/Tests/Fuzzers/FuzzWriter.cpp
Line
Count
Source
1
#include <functional>
2
#include <Packet.h>
3
#include <PcapFileDevice.h>
4
5
#include "Logger.h"
6
#include "DumpToFile.h"
7
8
static std::string tmpName;
9
static std::string tmpFile;
10
static std::string outPcapFile;
11
static int writes = 0;
12
13
extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
14
0
{
15
0
  if (tmpName.empty())
16
0
    tmpName = tmpnam(nullptr);
17
18
0
  if (tmpFile.empty())
19
0
    tmpFile = tmpName + FILE_EXT;
20
21
0
  if (dumpDataToPcapFile(data, size, tmpFile.c_str()) != 0)
22
0
  {
23
0
    std::cerr << "Can't Dump buffer to the '" << tmpFile << "' file!!!!\n";
24
0
    return -1;
25
0
  }
26
27
0
  pcpp::Logger::getInstance().suppressLogs();
28
29
0
  std::unique_ptr<pcpp::IFileReaderDevice> reader(pcpp::IFileReaderDevice::getReader(tmpFile));
30
0
  if (!reader->open())
31
0
  {
32
0
    std::cerr << "Error opening the '" << tmpFile << "' file\n";
33
0
    return -1;
34
0
  }
35
36
0
  if (outPcapFile.empty())
37
0
#ifdef NG_WRITER
38
0
    outPcapFile = tmpName + ".pcapng";
39
#else
40
    outPcapFile = tmpName + ".pcap";
41
#endif
42
43
0
#ifdef NG_WRITER
44
0
  pcpp::PcapNgFileWriterDevice pcapWriter(outPcapFile);
45
#else
46
  pcpp::PcapFileWriterDevice pcapWriter(outPcapFile, pcpp::LINKTYPE_ETHERNET);
47
#endif
48
0
  if (writes++ == 10)
49
0
  {
50
0
    writes = 1;
51
0
    remove(outPcapFile.c_str());
52
0
  }
53
0
  if (!pcapWriter.open(writes != 1))
54
0
  {
55
0
    std::cerr << "Cannot open '" << outPcapFile << "' for writing" << std::endl;
56
0
    return -1;
57
0
  }
58
59
0
  pcpp::RawPacketVector packets;
60
0
  if (reader->getNextPackets(packets, 1) != 1)
61
0
  {
62
0
    std::cerr << "Couldn't read the first packet in the file\n";
63
0
    return 0;
64
0
  }
65
66
0
  pcpp::RawPacket& rawPacket = *packets.front();
67
0
  do
68
0
  {
69
0
    pcapWriter.writePacket(rawPacket);
70
0
  } while (reader->getNextPacket(rawPacket));
71
72
0
  pcpp::PcapStats stats;
73
0
  pcapWriter.getStatistics(stats);
74
0
  std::cout << "Written " << stats.packetsRecv << " packets successfully to pcap writer and " << stats.packetsDrop
75
0
            << " packets could not be written" << std::endl;
76
77
0
  pcapWriter.close();
78
0
  return 0;
79
0
}