Skip to content

Commit 6dd7c51

Browse files
authored
Changed IPNetwork copy assignment implementation to copy-and-swap. (#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.
1 parent 91d2058 commit 6dd7c51

File tree

1 file changed

+4
-22
lines changed

1 file changed

+4
-22
lines changed

Common++/header/IpAddress.h

Lines changed: 4 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -707,18 +707,9 @@ namespace pcpp
707707
/// @return A reference to the assignee
708708
IPNetwork& operator=(const IPv4Network& other)
709709
{
710-
if (m_IPv4Network)
711-
{
712-
m_IPv4Network = nullptr;
713-
}
714-
715-
if (m_IPv6Network)
716-
{
717-
m_IPv6Network = nullptr;
718-
}
719-
710+
// Create the new instance first to maintain strong exception guarantee.
720711
m_IPv4Network = std::unique_ptr<IPv4Network>(new IPv4Network(other));
721-
712+
m_IPv6Network = nullptr;
722713
return *this;
723714
}
724715

@@ -727,18 +718,9 @@ namespace pcpp
727718
/// @return A reference to the assignee
728719
IPNetwork& operator=(const IPv6Network& other)
729720
{
730-
if (m_IPv4Network)
731-
{
732-
m_IPv4Network = nullptr;
733-
}
734-
735-
if (m_IPv6Network)
736-
{
737-
m_IPv6Network = nullptr;
738-
}
739-
721+
// Create the new instance first to maintain strong exception guarantee.
740722
m_IPv6Network = std::unique_ptr<IPv6Network>(new IPv6Network(other));
741-
723+
m_IPv4Network = nullptr;
742724
return *this;
743725
}
744726

0 commit comments

Comments
 (0)