Skip to content

Commit 8f27144

Browse files
Refactor: Use std::make_unique where applicable (#1854)
* Refactor: Use std::make_unique where applicable Replaced direct usage of new with std::unique_ptr with std::make_unique for improved memory safety and conciseness in C++14 and later. Introduced static factory methods for PcapLiveDevice and WinPcapLiveDevice to allow std::unique_ptr creation while keeping their constructors protected. * fix * format code. * revert --------- Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
1 parent 0eea5c8 commit 8f27144

File tree

10 files changed

+36
-34
lines changed

10 files changed

+36
-34
lines changed

Common++/header/IpAddress.h

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -625,11 +625,11 @@ namespace pcpp
625625
{
626626
if (address.isIPv4())
627627
{
628-
m_IPv4Network = std::unique_ptr<IPv4Network>(new IPv4Network(address.getIPv4(), prefixLen));
628+
m_IPv4Network = std::make_unique<IPv4Network>(address.getIPv4(), prefixLen);
629629
}
630630
else
631631
{
632-
m_IPv6Network = std::unique_ptr<IPv6Network>(new IPv6Network(address.getIPv6(), prefixLen));
632+
m_IPv6Network = std::make_unique<IPv6Network>(address.getIPv6(), prefixLen);
633633
}
634634
}
635635

@@ -646,11 +646,11 @@ namespace pcpp
646646
{
647647
if (address.isIPv4())
648648
{
649-
m_IPv4Network = std::unique_ptr<IPv4Network>(new IPv4Network(address.getIPv4(), netmask));
649+
m_IPv4Network = std::make_unique<IPv4Network>(address.getIPv4(), netmask);
650650
}
651651
else
652652
{
653-
m_IPv6Network = std::unique_ptr<IPv6Network>(new IPv6Network(address.getIPv6(), netmask));
653+
m_IPv6Network = std::make_unique<IPv6Network>(address.getIPv6(), netmask);
654654
}
655655
}
656656

@@ -666,11 +666,11 @@ namespace pcpp
666666
{
667667
try
668668
{
669-
m_IPv4Network = std::unique_ptr<IPv4Network>(new IPv4Network(addressAndNetmask));
669+
m_IPv4Network = std::make_unique<IPv4Network>(addressAndNetmask);
670670
}
671671
catch (const std::invalid_argument&)
672672
{
673-
m_IPv6Network = std::unique_ptr<IPv6Network>(new IPv6Network(addressAndNetmask));
673+
m_IPv6Network = std::make_unique<IPv6Network>(addressAndNetmask);
674674
}
675675
}
676676

@@ -680,12 +680,12 @@ namespace pcpp
680680
{
681681
if (other.m_IPv4Network)
682682
{
683-
m_IPv4Network = std::unique_ptr<IPv4Network>(new IPv4Network(*other.m_IPv4Network));
683+
m_IPv4Network = std::make_unique<IPv4Network>(*other.m_IPv4Network);
684684
}
685685

686686
if (other.m_IPv6Network)
687687
{
688-
m_IPv6Network = std::unique_ptr<IPv6Network>(new IPv6Network(*other.m_IPv6Network));
688+
m_IPv6Network = std::make_unique<IPv6Network>(*other.m_IPv6Network);
689689
}
690690
}
691691

@@ -710,7 +710,7 @@ namespace pcpp
710710
IPNetwork& operator=(const IPv4Network& other)
711711
{
712712
// Create the new instance first to maintain strong exception guarantee.
713-
m_IPv4Network = std::unique_ptr<IPv4Network>(new IPv4Network(other));
713+
m_IPv4Network = std::make_unique<IPv4Network>(other);
714714
m_IPv6Network = nullptr;
715715
return *this;
716716
}
@@ -721,7 +721,7 @@ namespace pcpp
721721
IPNetwork& operator=(const IPv6Network& other)
722722
{
723723
// Create the new instance first to maintain strong exception guarantee.
724-
m_IPv6Network = std::unique_ptr<IPv6Network>(new IPv6Network(other));
724+
m_IPv6Network = std::make_unique<IPv6Network>(other);
725725
m_IPv4Network = nullptr;
726726
return *this;
727727
}

Common++/header/PointerVector.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ namespace pcpp
2525
{
2626
std::unique_ptr<T> operator()(const T& obj) const
2727
{
28-
return std::unique_ptr<T>(new T(obj));
28+
return std::make_unique<T>(obj);
2929
}
3030
};
3131

Common++/src/Logger.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ namespace pcpp
6767
ctx->init(level, source);
6868
return ctx;
6969
}
70-
return std::unique_ptr<internal::LogContext>(new internal::LogContext(level, source));
70+
return std::make_unique<internal::LogContext>(level, source);
7171
}
7272

7373
void Logger::emit(std::unique_ptr<internal::LogContext> message)

Packet++/src/LdapLayer.cpp

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -205,13 +205,13 @@ namespace pcpp
205205
std::unique_ptr<Asn1Record> messageRootRecord;
206206
if (!messageRecords.empty())
207207
{
208-
messageRootRecord = std::unique_ptr<Asn1Record>(
209-
new Asn1ConstructedRecord(Asn1TagClass::Application, operationType, messageRecords));
208+
messageRootRecord =
209+
std::make_unique<Asn1ConstructedRecord>(Asn1TagClass::Application, operationType, messageRecords);
210210
}
211211
else
212212
{
213213
messageRootRecord =
214-
std::unique_ptr<Asn1Record>(new Asn1GenericRecord(Asn1TagClass::Application, false, operationType, ""));
214+
std::make_unique<Asn1GenericRecord>(Asn1TagClass::Application, false, operationType, "");
215215
}
216216

217217
std::vector<Asn1Record*> rootSubRecords = { &messageIdRecord, messageRootRecord.get() };
@@ -230,14 +230,14 @@ namespace pcpp
230230
else
231231
{
232232
auto controlValueSize = static_cast<size_t>(control.controlValue.size() / 2);
233-
std::unique_ptr<uint8_t[]> controlValue(new uint8_t[controlValueSize]);
233+
std::unique_ptr<uint8_t[]> controlValue = std::make_unique<uint8_t[]>(controlValueSize);
234234
controlValueSize = hexStringToByteArray(control.controlValue, controlValue.get(), controlValueSize);
235235
Asn1OctetStringRecord controlValueRecord(controlValue.get(), controlValueSize);
236236
controlsSubRecords.pushBack(new Asn1SequenceRecord({ &controlTypeRecord, &controlValueRecord }));
237237
}
238238
}
239-
controlsRecord = std::unique_ptr<Asn1ConstructedRecord>(
240-
new Asn1ConstructedRecord(Asn1TagClass::ContextSpecific, 0, controlsSubRecords));
239+
controlsRecord =
240+
std::make_unique<Asn1ConstructedRecord>(Asn1TagClass::ContextSpecific, 0, controlsSubRecords);
241241
rootSubRecords.push_back(controlsRecord.get());
242242
}
243243

@@ -404,8 +404,8 @@ namespace pcpp
404404
{
405405
referralSubRecords.pushBack(new Asn1OctetStringRecord(uri));
406406
}
407-
referralRecord = std::unique_ptr<Asn1ConstructedRecord>(
408-
new Asn1ConstructedRecord(Asn1TagClass::ContextSpecific, referralTagType, referralSubRecords));
407+
referralRecord = std::make_unique<Asn1ConstructedRecord>(Asn1TagClass::ContextSpecific, referralTagType,
408+
referralSubRecords);
409409
messageRecords.push_back(referralRecord.get());
410410
}
411411

@@ -489,10 +489,10 @@ namespace pcpp
489489
if (!simpleAuthentication.empty())
490490
{
491491
auto data = reinterpret_cast<const uint8_t*>(simpleAuthentication.data());
492-
simpleAuthenticationRecord = std::unique_ptr<Asn1GenericRecord>(
493-
new Asn1GenericRecord(Asn1TagClass::ContextSpecific, false,
494-
static_cast<uint8_t>(LdapBindRequestLayer::AuthenticationType::Simple), data,
495-
simpleAuthentication.size()));
492+
simpleAuthenticationRecord = std::make_unique<Asn1GenericRecord>(
493+
Asn1TagClass::ContextSpecific, false,
494+
static_cast<uint8_t>(LdapBindRequestLayer::AuthenticationType::Simple), data,
495+
simpleAuthentication.size());
496496
messageRecords.push_back(simpleAuthenticationRecord.get());
497497
}
498498

@@ -518,9 +518,9 @@ namespace pcpp
518518
saslAuthenticationRecords.pushBack(credentialsRecord);
519519
}
520520

521-
saslAuthenticationRecord = std::unique_ptr<Asn1ConstructedRecord>(new Asn1ConstructedRecord(
521+
saslAuthenticationRecord = std::make_unique<Asn1ConstructedRecord>(
522522
Asn1TagClass::ContextSpecific, static_cast<uint8_t>(LdapBindRequestLayer::AuthenticationType::Sasl),
523-
saslAuthenticationRecords));
523+
saslAuthenticationRecords);
524524
messageRecords.push_back(saslAuthenticationRecord.get());
525525
}
526526

@@ -625,9 +625,9 @@ namespace pcpp
625625
std::unique_ptr<Asn1Record> serverSaslCredentialsRecord;
626626
if (!serverSaslCredentials.empty())
627627
{
628-
serverSaslCredentialsRecord = std::unique_ptr<Asn1Record>(
629-
new Asn1GenericRecord(Asn1TagClass::ContextSpecific, false, serverSaslCredentialsTagType,
630-
serverSaslCredentials.data(), serverSaslCredentials.size()));
628+
serverSaslCredentialsRecord =
629+
std::make_unique<Asn1GenericRecord>(Asn1TagClass::ContextSpecific, false, serverSaslCredentialsTagType,
630+
serverSaslCredentials.data(), serverSaslCredentials.size());
631631
additionalRecords.push_back(serverSaslCredentialsRecord.get());
632632
}
633633

Pcap++/src/DpdkDeviceList.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -198,6 +198,7 @@ namespace pcpp
198198
// Initialize a DpdkDevice per port
199199
for (int i = 0; i < numOfPorts; i++)
200200
{
201+
// Not using std::make_unique because ctor of DpdkDevice is private
201202
auto newDevice = std::unique_ptr<DpdkDevice>(new DpdkDevice(i, mBufPoolSizePerDevice, mBufDataSize));
202203
PCPP_LOG_DEBUG("DpdkDevice #" << i << ": Name='" << newDevice->getDeviceName() << "', PCI-slot='"
203204
<< newDevice->getPciAddress() << "', PMD='" << newDevice->getPMDName()

Pcap++/src/PcapFileDevice.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -338,7 +338,7 @@ namespace pcpp
338338
{
339339
return false;
340340
}
341-
std::unique_ptr<char[]> packetData(new char[packetSize]);
341+
std::unique_ptr<char[]> packetData = std::make_unique<char[]>(packetSize);
342342
m_snoopFile.read(packetData.get(), packetSize);
343343
if (!m_snoopFile)
344344
{

Pcap++/src/PcapFilter.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ namespace pcpp
6868
return false;
6969
}
7070

71-
std::unique_ptr<bpf_program> newProg = std::unique_ptr<bpf_program>(new bpf_program);
71+
auto newProg = std::make_unique<bpf_program>();
7272
int ret = pcap_compile(pcap.get(), newProg.get(), filter.c_str(), 1, 0);
7373
if (ret < 0)
7474
{

Pcap++/src/PcapLiveDevice.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -688,8 +688,8 @@ namespace pcpp
688688
{
689689
// Due to passing a 'this' pointer, the current device object shouldn't be relocated, while the worker is
690690
// active.
691-
m_StatisticsUpdateWorker = std::unique_ptr<StatisticsUpdateWorker>(new StatisticsUpdateWorker(
692-
*this, std::move(onStatsUpdate), onStatsUpdateUserCookie, intervalInSecondsToUpdateStats * 1000));
691+
m_StatisticsUpdateWorker = std::make_unique<StatisticsUpdateWorker>(
692+
*this, std::move(onStatsUpdate), onStatsUpdateUserCookie, intervalInSecondsToUpdateStats * 1000);
693693

694694
PCPP_LOG_DEBUG("Successfully created stats thread for device '" << m_InterfaceDetails.name << "'.");
695695
}

Pcap++/src/PfRingDeviceList.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ namespace pcpp
9292
PCPP_LOG_DEBUG("PF_RING version is: " << m_PfRingVersion);
9393
}
9494

95+
// Not using std::make_unique because ctor of PfRingDevice is private
9596
auto newDev = std::unique_ptr<PfRingDevice>(new PfRingDevice(currInterface->name));
9697
m_DeviceList.pushBack(std::move(newDev));
9798
PCPP_LOG_DEBUG("Found interface: " << currInterface->name);

Tests/Packet++Test/Tests/PacketTests.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -818,7 +818,7 @@ PTF_TEST_CASE(PacketTrailerTest)
818818

819819
// add layer after trailer (result with an error)
820820
uint8_t payload[4] = { 0x1, 0x2, 0x3, 0x4 };
821-
std::unique_ptr<pcpp::PayloadLayer> newPayloadLayer(new pcpp::PayloadLayer(payload, 4));
821+
std::unique_ptr<pcpp::PayloadLayer> newPayloadLayer = std::make_unique<pcpp::PayloadLayer>(payload, 4);
822822
pcpp::Logger::getInstance().suppressLogs();
823823
PTF_ASSERT_FALSE(trailerIPv4Packet.addLayer(newPayloadLayer.get(), true));
824824
pcpp::Logger::getInstance().enableLogs();

0 commit comments

Comments
 (0)