Coverage Report

Created: 2026-05-30 07:22

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/PcapPlusPlus/Packet++/src/Layer.cpp
Line
Count
Source
1
3.86k
#define LOG_MODULE PacketLogModuleLayer
2
3
#include "Layer.h"
4
#include "Logger.h"
5
#include "Packet.h"
6
#include <cstring>
7
8
namespace pcpp
9
{
10
11
  Layer::~Layer()
12
5.15M
  {
13
5.15M
    if (!isAllocatedToPacket())
14
131k
      delete[] m_Data;
15
5.15M
  }
16
17
131k
  Layer::Layer(const Layer& other) : m_Protocol(other.m_Protocol), m_NextLayer(nullptr), m_PrevLayer(nullptr)
18
131k
  {
19
131k
    m_DataLen = other.getHeaderLen();
20
131k
    m_Data = new uint8_t[other.m_DataLen];
21
131k
    memcpy(m_Data, other.m_Data, other.m_DataLen);
22
131k
  }
23
24
  Layer& Layer::operator=(const Layer& other)
25
14.9k
  {
26
14.9k
    if (this == &other)
27
0
      return *this;
28
29
    // Should this really always delete m_Data? What if the layer is attached to a packet?
30
14.9k
    if (m_Data != nullptr)
31
14.9k
      delete[] m_Data;
32
33
    // Reset allocation info as the layer is considered copied and not attached to any packet.
34
14.9k
    m_AllocationInfo = internal::LayerAllocationInfo{};
35
36
14.9k
    m_DataLen = other.getHeaderLen();
37
14.9k
    m_Protocol = other.m_Protocol;
38
14.9k
    m_NextLayer = nullptr;
39
14.9k
    m_PrevLayer = nullptr;
40
14.9k
    m_Data = new uint8_t[other.m_DataLen];
41
14.9k
    memcpy(m_Data, other.m_Data, other.m_DataLen);
42
43
14.9k
    return *this;
44
14.9k
  }
45
46
  bool Layer::isMemberOfProtocolFamily(ProtocolTypeFamily protocolTypeFamily) const
47
17.0M
  {
48
17.0M
    return m_Protocol != UnknownProtocol && internal::protoFamilyContainsProtocol(protocolTypeFamily, m_Protocol);
49
17.0M
  }
50
51
  void Layer::copyData(uint8_t* toArr) const
52
0
  {
53
0
    memcpy(toArr, m_Data, m_DataLen);
54
0
  }
55
56
  bool Layer::extendLayer(int offsetInLayer, size_t numOfBytesToExtend)
57
228k
  {
58
228k
    if (m_Data == nullptr)
59
0
    {
60
0
      PCPP_LOG_ERROR("Layer's data is nullptr");
61
0
      return false;
62
0
    }
63
64
228k
    if (getAttachedPacket() == nullptr)
65
127k
    {
66
127k
      if (static_cast<size_t>(offsetInLayer) > m_DataLen)
67
1.92k
      {
68
1.92k
        PCPP_LOG_ERROR("Requested offset is larger than data length");
69
1.92k
        return false;
70
1.92k
      }
71
125k
      uint8_t* newData = new uint8_t[m_DataLen + numOfBytesToExtend];
72
125k
      memcpy(newData, m_Data, offsetInLayer);
73
125k
      memcpy(newData + offsetInLayer + numOfBytesToExtend, m_Data + offsetInLayer, m_DataLen - offsetInLayer);
74
125k
      delete[] m_Data;
75
125k
      m_Data = newData;
76
125k
      m_DataLen += numOfBytesToExtend;
77
125k
      return true;
78
127k
    }
79
80
100k
    return getAttachedPacket()->extendLayer(this, offsetInLayer, numOfBytesToExtend);
81
228k
  }
82
83
  bool Layer::shortenLayer(int offsetInLayer, size_t numOfBytesToShorten)
84
99.7k
  {
85
99.7k
    if (m_Data == nullptr)
86
0
    {
87
0
      PCPP_LOG_ERROR("Layer's data is nullptr");
88
0
      return false;
89
0
    }
90
91
99.7k
    if (static_cast<size_t>(offsetInLayer) + numOfBytesToShorten > m_DataLen)
92
5
    {
93
5
      PCPP_LOG_ERROR("Requested number of bytes to shorten is larger than data length");
94
5
      return false;
95
5
    }
96
97
99.7k
    if (getAttachedPacket() == nullptr)
98
8.34k
    {
99
8.34k
      if (static_cast<size_t>(offsetInLayer) >= m_DataLen)
100
0
      {
101
0
        PCPP_LOG_ERROR("Requested offset is larger than data length");
102
0
        return false;
103
0
      }
104
8.34k
      uint8_t* newData = new uint8_t[m_DataLen - numOfBytesToShorten];
105
8.34k
      memcpy(newData, m_Data, offsetInLayer);
106
8.34k
      memcpy(newData + offsetInLayer, m_Data + offsetInLayer + numOfBytesToShorten,
107
8.34k
             m_DataLen - offsetInLayer - numOfBytesToShorten);
108
8.34k
      delete[] m_Data;
109
8.34k
      m_Data = newData;
110
8.34k
      m_DataLen -= numOfBytesToShorten;
111
8.34k
      return true;
112
8.34k
    }
113
114
91.4k
    return getAttachedPacket()->shortenLayer(this, offsetInLayer, numOfBytesToShorten);
115
99.7k
  }
116
117
}  // namespace pcpp