Skip to content

Commit 1eff7bb

Browse files
authored
some C++14 migration (#1953)
* some C++14 migration * format * more fix * more * code review * test
1 parent 07f33c0 commit 1eff7bb

20 files changed

+66
-64
lines changed

Packet++/header/IPReassembly.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -284,7 +284,7 @@ namespace pcpp
284284
/// @param[in] key A pointer to the identifier of the packet that is being dropped
285285
/// @param[in] userCookie A pointer to the cookie provided by the user in IPReassemby c'tor (or nullptr if no
286286
/// cookie provided)
287-
typedef void (*OnFragmentsClean)(const PacketKey* key, void* userCookie);
287+
using OnFragmentsClean = void (*)(const PacketKey* key, void* userCookie);
288288

289289
/// An enum representing the status returned from processing a fragment
290290
enum ReassemblyStatus

Packet++/header/ProtocolType.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ namespace pcpp
3232

3333
/// @typedef ProtocolType
3434
/// Representing all protocols supported by PcapPlusPlus
35-
typedef uint8_t ProtocolType;
35+
using ProtocolType = uint8_t;
3636

3737
/// @typedef ProtocolTypeFamily
3838
/// Representing a family of protocols
39-
typedef uint32_t ProtocolTypeFamily;
39+
using ProtocolTypeFamily = uint32_t;
4040

4141
/// Unknown protocol (or unsupported by PcapPlusPlus)
4242
const ProtocolType UnknownProtocol = 0;

Packet++/header/SomeIpSdLayer.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -508,10 +508,10 @@ namespace pcpp
508508
public:
509509
friend class SomeIpSdEntry;
510510

511-
typedef SomeIpSdEntry* EntryPtr;
512-
typedef std::vector<EntryPtr> EntriesVec;
513-
typedef SomeIpSdOption* OptionPtr;
514-
typedef std::vector<OptionPtr> OptionsVec;
511+
using EntryPtr = SomeIpSdEntry*;
512+
using EntriesVec = std::vector<EntryPtr>;
513+
using OptionPtr = SomeIpSdOption*;
514+
using OptionsVec = std::vector<OptionPtr>;
515515

516516
/// A constructor that creates the layer from an existing packet raw data
517517
/// @param[in] data A pointer to the raw data

Packet++/header/TcpReassembly.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,7 +308,7 @@ namespace pcpp
308308
};
309309

310310
/// The type for storing the connection information
311-
typedef std::unordered_map<uint32_t, ConnectionData> ConnectionInfoList;
311+
using ConnectionInfoList = std::unordered_map<uint32_t, ConnectionData>;
312312

313313
/// @typedef OnTcpMessageReady
314314
/// A callback invoked when new data arrives on a connection
@@ -459,8 +459,8 @@ namespace pcpp
459459
OutOfOrderProcessingGuard& operator=(const OutOfOrderProcessingGuard&) = delete;
460460
};
461461

462-
typedef std::unordered_map<uint32_t, TcpReassemblyData> ConnectionList;
463-
typedef std::map<time_t, std::list<uint32_t>> CleanupList;
462+
using ConnectionList = std::unordered_map<uint32_t, TcpReassemblyData>;
463+
using CleanupList = std::map<time_t, std::list<uint32_t>>;
464464

465465
OnTcpMessageReady m_OnMessageReadyCallback;
466466
OnTcpConnectionStart m_OnConnStart;

Packet++/src/DnsResource.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -196,8 +196,8 @@ namespace pcpp
196196

197197
DnsType IDnsResource::getDnsType() const
198198
{
199-
uint16_t dnsType = *(uint16_t*)(getRawData() + m_NameLength);
200-
return (DnsType)be16toh(dnsType);
199+
uint16_t dnsType = *reinterpret_cast<uint16_t*>(getRawData() + m_NameLength);
200+
return static_cast<DnsType>(be16toh(dnsType));
201201
}
202202

203203
void IDnsResource::setDnsType(DnsType newType)
@@ -208,8 +208,8 @@ namespace pcpp
208208

209209
DnsClass IDnsResource::getDnsClass() const
210210
{
211-
uint16_t dnsClass = *(uint16_t*)(getRawData() + m_NameLength + sizeof(uint16_t));
212-
return (DnsClass)be16toh(dnsClass);
211+
uint16_t dnsClass = *reinterpret_cast<uint16_t*>(getRawData() + m_NameLength + sizeof(uint16_t));
212+
return static_cast<DnsClass>(be16toh(dnsClass));
213213
}
214214

215215
void IDnsResource::setDnsClass(DnsClass newClass)
@@ -268,7 +268,7 @@ namespace pcpp
268268

269269
uint32_t DnsResource::getTTL() const
270270
{
271-
uint32_t ttl = *(uint32_t*)(getRawData() + m_NameLength + 2 * sizeof(uint16_t));
271+
uint32_t ttl = *reinterpret_cast<uint32_t*>(getRawData() + m_NameLength + 2 * sizeof(uint16_t));
272272
return be32toh(ttl);
273273
}
274274

@@ -290,7 +290,7 @@ namespace pcpp
290290
return 0;
291291
}
292292

293-
uint16_t dataLength = *(uint16_t*)(getRawData() + sizeToRead);
293+
uint16_t dataLength = *reinterpret_cast<uint16_t*>(getRawData() + sizeToRead);
294294
return be16toh(dataLength);
295295
}
296296

@@ -444,7 +444,7 @@ namespace pcpp
444444

445445
uint16_t DnsResource::getCustomDnsClass() const
446446
{
447-
uint16_t value = *(uint16_t*)(getRawData() + m_NameLength + sizeof(uint16_t));
447+
uint16_t value = *reinterpret_cast<uint16_t*>(getRawData() + m_NameLength + sizeof(uint16_t));
448448
return be16toh(value);
449449
}
450450

Packet++/src/IgmpLayer.cpp

Lines changed: 13 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ namespace pcpp
8888
uint16_t IgmpLayer::calculateChecksum()
8989
{
9090
ScalarBuffer<uint16_t> buffer;
91-
buffer.buffer = (uint16_t*)getIgmpHeader();
91+
buffer.buffer = reinterpret_cast<uint16_t*>(getIgmpHeader());
9292
buffer.len = getHeaderLen();
9393
return computeChecksum(&buffer, 1);
9494
}
@@ -224,7 +224,7 @@ namespace pcpp
224224
return IPv4Address();
225225

226226
uint8_t* ptr = m_Data + ptrOffset;
227-
return IPv4Address(*(uint32_t*)ptr);
227+
return IPv4Address(*reinterpret_cast<uint32_t*>(ptr));
228228
}
229229

230230
size_t IgmpV3QueryLayer::getHeaderLen() const
@@ -256,7 +256,7 @@ namespace pcpp
256256
{
257257
uint16_t sourceAddrCount = getSourceAddressCount();
258258

259-
if (index < 0 || index > (int)sourceAddrCount)
259+
if (index < 0 || index > static_cast<int>(sourceAddrCount))
260260
{
261261
PCPP_LOG_ERROR("Cannot add source address at index " << index << ", index is out of bounds");
262262
return false;
@@ -286,7 +286,7 @@ namespace pcpp
286286
{
287287
uint16_t sourceAddrCount = getSourceAddressCount();
288288

289-
if (index < 0 || index > (int)sourceAddrCount - 1)
289+
if (index < 0 || index > static_cast<int>(sourceAddrCount) - 1)
290290
{
291291
PCPP_LOG_ERROR("Cannot remove source address at index " << index << ", index is out of bounds");
292292
return false;
@@ -348,11 +348,13 @@ namespace pcpp
348348
if (groupRecord == nullptr)
349349
return nullptr;
350350

351-
// prev group was the last group
352-
if ((uint8_t*)groupRecord + groupRecord->getRecordLen() - m_Data >= (int)getHeaderLen())
351+
uint8_t* nextGroupRecordBegin = reinterpret_cast<uint8_t*>(groupRecord) + groupRecord->getRecordLen();
352+
if (std::distance(m_Data, nextGroupRecordBegin) >= static_cast<std::ptrdiff_t>(getHeaderLen()))
353+
{
353354
return nullptr;
355+
}
354356

355-
igmpv3_group_record* nextGroup = (igmpv3_group_record*)((uint8_t*)groupRecord + groupRecord->getRecordLen());
357+
igmpv3_group_record* nextGroup = reinterpret_cast<igmpv3_group_record*>(nextGroupRecordBegin);
356358

357359
return nextGroup;
358360
}
@@ -368,7 +370,7 @@ namespace pcpp
368370
const std::vector<IPv4Address>& sourceAddresses,
369371
int offset)
370372
{
371-
if (offset > (int)getHeaderLen())
373+
if (offset > static_cast<int>(getHeaderLen()))
372374
{
373375
PCPP_LOG_ERROR("Cannot add group record, offset is out of layer bounds");
374376
return nullptr;
@@ -403,13 +405,13 @@ namespace pcpp
403405

404406
getReportHeader()->numOfGroupRecords = htobe16(getGroupRecordCount() + 1);
405407

406-
return (igmpv3_group_record*)(m_Data + offset);
408+
return reinterpret_cast<igmpv3_group_record*>(m_Data + offset);
407409
}
408410

409411
igmpv3_group_record* IgmpV3ReportLayer::addGroupRecord(uint8_t recordType, const IPv4Address& multicastAddress,
410412
const std::vector<IPv4Address>& sourceAddresses)
411413
{
412-
return addGroupRecordAt(recordType, multicastAddress, sourceAddresses, (int)getHeaderLen());
414+
return addGroupRecordAt(recordType, multicastAddress, sourceAddresses, static_cast<int>(getHeaderLen()));
413415
}
414416

415417
igmpv3_group_record* IgmpV3ReportLayer::addGroupRecordAtIndex(uint8_t recordType,
@@ -509,7 +511,7 @@ namespace pcpp
509511

510512
int offset = index * sizeof(uint32_t);
511513
const uint8_t* ptr = sourceAddresses + offset;
512-
return IPv4Address(*(uint32_t*)ptr);
514+
return IPv4Address(*reinterpret_cast<const uint32_t*>(ptr));
513515
}
514516

515517
size_t igmpv3_group_record::getRecordLen() const

Packet++/src/PPPoELayer.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -256,7 +256,7 @@ namespace pcpp
256256
if (m_Data == nullptr)
257257
return PPPoEDiscoveryLayer::PPPoETagTypes::PPPOE_TAG_EOL;
258258

259-
return (PPPoEDiscoveryLayer::PPPoETagTypes)be16toh(m_Data->recordType);
259+
return static_cast<PPPoEDiscoveryLayer::PPPoETagTypes>(be16toh(m_Data->recordType));
260260
}
261261

262262
size_t PPPoEDiscoveryLayer::PPPoETag::getTotalSize() const

Packet++/src/PacketUtils.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ namespace pcpp
2828
if (vec[i].len % 2)
2929
{
3030
// access to the last byte using an uint8_t pointer
31-
uint8_t* vecBytes = (uint8_t*)vec[i].buffer;
31+
uint8_t* vecBytes = reinterpret_cast<uint8_t*>(vec[i].buffer);
3232
uint8_t lastByte = vecBytes[vec[i].len - 1];
3333
PCPP_LOG_DEBUG("1 byte left, adding value: 0x" << std::uppercase << std::hex << lastByte);
3434
// We have read the latest byte manually but this byte should be properly interpreted
@@ -72,7 +72,7 @@ namespace pcpp
7272

7373
uint16_t checksumRes = 0;
7474
ScalarBuffer<uint16_t> vec[2];
75-
vec[0].buffer = (uint16_t*)dataPtr;
75+
vec[0].buffer = reinterpret_cast<uint16_t*>(dataPtr);
7676
vec[0].len = dataLen;
7777

7878
if (ipAddrType == IPAddress::IPv4AddressType)
@@ -93,8 +93,8 @@ namespace pcpp
9393
else if (ipAddrType == IPAddress::IPv6AddressType)
9494
{
9595
uint16_t pseudoHeader[18];
96-
srcIPAddress.getIPv6().copyTo((uint8_t*)pseudoHeader);
97-
dstIPAddress.getIPv6().copyTo((uint8_t*)(pseudoHeader + 8));
96+
srcIPAddress.getIPv6().copyTo(reinterpret_cast<uint8_t*>(pseudoHeader));
97+
dstIPAddress.getIPv6().copyTo(reinterpret_cast<uint8_t*>(pseudoHeader + 8));
9898
pseudoHeader[16] = 0xffff & htobe16(dataLen);
9999
pseudoHeader[17] = htobe16(0x00ff & protocolType);
100100
vec[1].buffer = pseudoHeader;

Packet++/src/SSHLayer.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -191,7 +191,7 @@ namespace pcpp
191191
if (offset + sizeof(uint32_t) >= m_DataLen)
192192
return;
193193

194-
size_t fieldLength = static_cast<size_t>(be32toh(*(uint32_t*)(m_Data + offset)));
194+
size_t fieldLength = static_cast<size_t>(be32toh(*reinterpret_cast<uint32_t*>(m_Data + offset)));
195195
if (offset + sizeof(uint32_t) + fieldLength > m_DataLen)
196196
return;
197197

@@ -215,7 +215,7 @@ namespace pcpp
215215
return "";
216216

217217
size_t fieldOffset = m_FieldOffsets[fieldOffsetIndex];
218-
uint32_t fieldLength = be32toh(*(uint32_t*)(m_Data + fieldOffset));
218+
uint32_t fieldLength = be32toh(*reinterpret_cast<uint32_t*>(m_Data + fieldOffset));
219219
return std::string(reinterpret_cast<const char*>(m_Data + fieldOffset + sizeof(uint32_t)), (size_t)fieldLength);
220220
}
221221

Packet++/src/SSLHandshake.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -693,7 +693,7 @@ namespace pcpp
693693
static uint32_t hashString(std::string str)
694694
{
695695
unsigned h = FIRST_HASH;
696-
for (std::string::size_type i = 0; i < str.size(); ++i)
696+
for (auto i = 0u; i < str.size(); ++i)
697697
{
698698
h = (h * A) ^ (str[i] * B);
699699
}
@@ -1127,7 +1127,7 @@ namespace pcpp
11271127
}
11281128

11291129
uint8_t* hostNameLengthPos = extensionDataPtr + sizeof(uint16_t) + sizeof(uint8_t);
1130-
uint16_t hostNameLength = be16toh(*(uint16_t*)hostNameLengthPos);
1130+
uint16_t hostNameLength = be16toh(*reinterpret_cast<uint16_t*>(hostNameLengthPos));
11311131

11321132
char* hostNameAsCharArr = new char[hostNameLength + 1];
11331133
memset(hostNameAsCharArr, 0, hostNameLength + 1);
@@ -1148,7 +1148,7 @@ namespace pcpp
11481148
uint16_t extensionLength = getLength();
11491149
if (extensionLength == 2) // server hello message
11501150
{
1151-
result.push_back(SSLVersion(be16toh(*(uint16_t*)getData())));
1151+
result.push_back(SSLVersion(be16toh(*reinterpret_cast<uint16_t*>(getData()))));
11521152
}
11531153
else // client-hello message
11541154
{
@@ -1159,7 +1159,7 @@ namespace pcpp
11591159
uint8_t* dataPtr = getData() + sizeof(uint8_t);
11601160
for (int i = 0; i < listLength / 2; i++)
11611161
{
1162-
result.push_back(SSLVersion(be16toh(*(uint16_t*)dataPtr)));
1162+
result.push_back(SSLVersion(be16toh(*reinterpret_cast<uint16_t*>(dataPtr))));
11631163
dataPtr += sizeof(uint16_t);
11641164
}
11651165
}

0 commit comments

Comments
 (0)