Skip to content

Commit 62e4f35

Browse files
ol-imorozkoseladb
andauthored
Fix memory management in RawPacket (#1709)
Ensure that the destructor and assignment operator correctly handle the deletion of raw data based on the m_DeleteRawDataAtDestructor flag by using the updated clear() method. Update comments to reflect these changes. Co-authored-by: seladb <pcapplusplus@gmail.com>
1 parent 49ac8ef commit 62e4f35

File tree

2 files changed

+8
-21
lines changed

2 files changed

+8
-21
lines changed

Packet++/header/RawPacket.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -311,9 +311,8 @@ namespace pcpp
311311
RawPacket(const RawPacket& other);
312312

313313
/// Assignment operator overload for this class. When using this operator on an already initialized RawPacket
314-
/// instance, the original raw data is freed first. Then the other instance is copied to this instance, the same
315-
/// way the copy constructor works
316-
/// @todo free raw data only if deleteRawDataAtDestructor was set to 'true'
314+
/// instance, the original raw data is freed first if deleteRawDataAtDestructor was set to 'true'.
315+
/// Then the other instance is copied to this instance, the same way the copy constructor works
317316
/// @param[in] other The instance to copy from
318317
RawPacket& operator=(const RawPacket& other);
319318

@@ -423,8 +422,7 @@ namespace pcpp
423422
}
424423

425424
/// Clears all members of this instance, meaning setting raw data to nullptr, raw data length to 0, etc.
426-
/// Currently raw data is always freed, even if deleteRawDataAtDestructor was set to 'false'
427-
/// @todo deleteRawDataAtDestructor was set to 'true', don't free the raw data
425+
/// Frees the raw data if deleteRawDataAtDestructor was set to 'true'
428426
/// @todo set timestamp to a default value as well
429427
virtual void clear();
430428

Packet++/src/RawPacket.cpp

Lines changed: 5 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,7 @@ namespace pcpp
4141

4242
RawPacket::~RawPacket()
4343
{
44-
if (m_DeleteRawDataAtDestructor)
45-
{
46-
delete[] m_RawData;
47-
}
44+
clear();
4845
}
4946

5047
RawPacket::RawPacket(const RawPacket& other)
@@ -57,10 +54,7 @@ namespace pcpp
5754
{
5855
if (this != &other)
5956
{
60-
if (m_RawData != nullptr)
61-
delete[] m_RawData;
62-
63-
m_RawPacketSet = false;
57+
clear();
6458

6559
copyDataFrom(other, true);
6660
}
@@ -104,14 +98,9 @@ namespace pcpp
10498
bool RawPacket::setRawData(const uint8_t* pRawData, int rawDataLen, timespec timestamp, LinkLayerType layerType,
10599
int frameLength)
106100
{
107-
if (frameLength == -1)
108-
frameLength = rawDataLen;
109-
m_FrameLength = frameLength;
110-
if (m_RawData != nullptr && m_DeleteRawDataAtDestructor)
111-
{
112-
delete[] m_RawData;
113-
}
101+
clear();
114102

103+
m_FrameLength = (frameLength == -1) ? rawDataLen : frameLength;
115104
m_RawData = (uint8_t*)pRawData;
116105
m_RawDataLen = rawDataLen;
117106
m_TimeStamp = timestamp;
@@ -129,7 +118,7 @@ namespace pcpp
129118

130119
void RawPacket::clear()
131120
{
132-
if (m_RawData != nullptr)
121+
if (m_RawData != nullptr && m_DeleteRawDataAtDestructor)
133122
delete[] m_RawData;
134123

135124
m_RawData = nullptr;

0 commit comments

Comments
 (0)