Skip to content

Commit

Permalink
Changed IPNetwork copy assignment implementation to copy-and-swap. (#…
Browse files Browse the repository at this point in the history
…1681)

* Changed `IPNetwork` copy assignment implementation to copy-and-swap the internal network.

This change fixes use of memory after being freed in case of self-assignment and provides a strong exception guarantee.

* Removed the temporary variable.
  • Loading branch information
Dimi1010 authored Jan 9, 2025
1 parent 91d2058 commit 6dd7c51
Showing 1 changed file with 4 additions and 22 deletions.
26 changes: 4 additions & 22 deletions Common++/header/IpAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -707,18 +707,9 @@ namespace pcpp
/// @return A reference to the assignee
IPNetwork& operator=(const IPv4Network& other)
{
if (m_IPv4Network)
{
m_IPv4Network = nullptr;
}

if (m_IPv6Network)
{
m_IPv6Network = nullptr;
}

// Create the new instance first to maintain strong exception guarantee.
m_IPv4Network = std::unique_ptr<IPv4Network>(new IPv4Network(other));

m_IPv6Network = nullptr;
return *this;
}

Expand All @@ -727,18 +718,9 @@ namespace pcpp
/// @return A reference to the assignee
IPNetwork& operator=(const IPv6Network& other)
{
if (m_IPv4Network)
{
m_IPv4Network = nullptr;
}

if (m_IPv6Network)
{
m_IPv6Network = nullptr;
}

// Create the new instance first to maintain strong exception guarantee.
m_IPv6Network = std::unique_ptr<IPv6Network>(new IPv6Network(other));

m_IPv4Network = nullptr;
return *this;
}

Expand Down

0 comments on commit 6dd7c51

Please sign in to comment.