From 514b25fce82763f32069f4a7f4f330eaec10b25d Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 3 Jun 2024 22:14:43 +0300 Subject: [PATCH 1/7] Fixed uninitialized variable. --- Pcap++/src/PcapLiveDevice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/src/PcapLiveDevice.cpp b/Pcap++/src/PcapLiveDevice.cpp index e368d71b5a..2f57af4490 100644 --- a/Pcap++/src/PcapLiveDevice.cpp +++ b/Pcap++/src/PcapLiveDevice.cpp @@ -75,7 +75,7 @@ static pcap_direction_t directionTypeMap(PcapLiveDevice::PcapDirection direction PcapLiveDevice::PcapLiveDevice(pcap_if_t* pInterface, bool calculateMTU, bool calculateMacAddress, bool calculateDefaultGateway) : - IPcapDevice(), m_PcapSelectableFd(-1), m_DefaultGateway(IPv4Address::Zero), m_UsePoll(false) + IPcapDevice(), m_PcapSendDescriptor(nullptr), m_PcapSelectableFd(-1), m_DefaultGateway(IPv4Address::Zero), m_UsePoll(false) { m_DeviceMtu = 0; m_LinkType = LINKTYPE_ETHERNET; From 3823a876e43b78a0d31142e51860e011f21e18d6 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 3 Jun 2024 22:16:26 +0300 Subject: [PATCH 2/7] Marked function as const. --- Pcap++/header/PcapLiveDevice.h | 2 +- Pcap++/src/PcapLiveDevice.cpp | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/Pcap++/header/PcapLiveDevice.h b/Pcap++/header/PcapLiveDevice.h index d6dd5756d3..e4b88ba2f0 100644 --- a/Pcap++/header/PcapLiveDevice.h +++ b/Pcap++/header/PcapLiveDevice.h @@ -438,7 +438,7 @@ namespace pcpp * @param[in] packetPayloadLength The length of the IP layer of the packet * @return True if the packetPayloadLength is less than or equal to the device MTU */ - bool doMtuCheck(int packetPayloadLength); + bool doMtuCheck(int packetPayloadLength) const; /** * Send a RawPacket to the network diff --git a/Pcap++/src/PcapLiveDevice.cpp b/Pcap++/src/PcapLiveDevice.cpp index 2f57af4490..7a55a0a2bf 100644 --- a/Pcap++/src/PcapLiveDevice.cpp +++ b/Pcap++/src/PcapLiveDevice.cpp @@ -677,7 +677,7 @@ void PcapLiveDevice::getStatistics(PcapStats& stats) const stats.packetsDropByInterface = pcapStats.ps_ifdrop; } -bool PcapLiveDevice::doMtuCheck(int packetPayloadLength) +bool PcapLiveDevice::doMtuCheck(int packetPayloadLength) const { if (packetPayloadLength > static_cast(m_DeviceMtu)) { From f1665dad7adf10d91ef1090ad50511e11a191714 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 3 Jun 2024 22:17:20 +0300 Subject: [PATCH 3/7] Fixed potential overflow warning. --- Pcap++/src/PcapRemoteDevice.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Pcap++/src/PcapRemoteDevice.cpp b/Pcap++/src/PcapRemoteDevice.cpp index b08733f10c..ac1ae9c46a 100644 --- a/Pcap++/src/PcapRemoteDevice.cpp +++ b/Pcap++/src/PcapRemoteDevice.cpp @@ -119,7 +119,7 @@ void PcapRemoteDevice::getStatistics(PcapStats& stats) const return; } stats.packetsRecv = tempStats->ps_capt; - stats.packetsDrop = tempStats->ps_drop + tempStats->ps_netdrop; + stats.packetsDrop = static_cast(tempStats->ps_drop) + tempStats->ps_netdrop; stats.packetsDropByInterface = tempStats->ps_ifdrop; } From 8a95276a914c9fc25e3418d19f2dbd3fac6015ee Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 3 Jun 2024 22:31:23 +0300 Subject: [PATCH 4/7] Replaced C-style casts -> Cpp casts. --- Packet++/header/IPv4Layer.h | 32 ++++++++++++++++---------------- 1 file changed, 16 insertions(+), 16 deletions(-) diff --git a/Packet++/header/IPv4Layer.h b/Packet++/header/IPv4Layer.h index b987090224..7e1e826333 100644 --- a/Packet++/header/IPv4Layer.h +++ b/Packet++/header/IPv4Layer.h @@ -242,9 +242,9 @@ namespace pcpp if (dataSize < 2) return res; - uint8_t valueOffset = (uint8_t)(1); + uint8_t valueOffset = static_cast(1); - while ((size_t)valueOffset < dataSize) + while (static_cast(valueOffset) < dataSize) { uint32_t curValue; memcpy(&curValue, m_Data->recordValue + valueOffset, sizeof(uint32_t)); @@ -253,7 +253,7 @@ namespace pcpp res.push_back(IPv4Address(curValue)); - valueOffset += (uint8_t)(4); + valueOffset += static_cast(4); } return res; @@ -281,12 +281,12 @@ namespace pcpp if (dataSize < 2) return res; - res.type = (IPv4TimestampOptionValue::TimestampType)m_Data->recordValue[1]; + res.type = static_cast(m_Data->recordValue[1]); - uint8_t valueOffset = (uint8_t)(2); + uint8_t valueOffset = static_cast(2); bool readIPAddr = (res.type == IPv4TimestampOptionValue::TimestampAndIP); - while ((size_t)valueOffset < dataSize) + while (static_cast(valueOffset) < dataSize) { uint32_t curValue; memcpy(&curValue, m_Data->recordValue + valueOffset, sizeof(uint32_t)); @@ -301,7 +301,7 @@ namespace pcpp if (res.type == IPv4TimestampOptionValue::TimestampAndIP) readIPAddr = !readIPAddr; - valueOffset += (uint8_t)(4); + valueOffset += static_cast(4); } return res; @@ -323,14 +323,14 @@ namespace pcpp */ static bool canAssign(const uint8_t* recordRawData, size_t tlvDataLen) { - auto data = (TLVRawData*)recordRawData; + auto data = reinterpret_cast(recordRawData); if (data == nullptr) return false; if (tlvDataLen < sizeof(TLVRawData::recordType)) return false; - if (getIPv4OptionType(data) == (uint8_t)IPV4OPT_EndOfOptionsList || data->recordType == (uint8_t)IPV4OPT_NOP) + if (getIPv4OptionType(data) == static_cast(IPV4OPT_EndOfOptionsList) || data->recordType == static_cast(IPV4OPT_NOP)) return true; return TLVRecord::canAssign(recordRawData, tlvDataLen); @@ -343,10 +343,10 @@ namespace pcpp if (m_Data == nullptr) return 0; - if (getIPv4OptionType() == (uint8_t)IPV4OPT_EndOfOptionsList || m_Data->recordType == (uint8_t)IPV4OPT_NOP) + if (getIPv4OptionType() == static_cast(IPV4OPT_EndOfOptionsList) || m_Data->recordType == static_cast(IPV4OPT_NOP)) return sizeof(uint8_t); - return (size_t)m_Data->recordLen; + return static_cast(m_Data->recordLen); } size_t getDataSize() const @@ -354,10 +354,10 @@ namespace pcpp if (m_Data == nullptr) return 0; - if (getIPv4OptionType() == (uint8_t)IPV4OPT_EndOfOptionsList || m_Data->recordType == (uint8_t)IPV4OPT_NOP) - return (size_t)0; + if (getIPv4OptionType() == static_cast(IPV4OPT_EndOfOptionsList) || m_Data->recordType == static_cast(IPV4OPT_NOP)) + return 0; - return (size_t)m_Data->recordLen - (2*sizeof(uint8_t)); + return static_cast(m_Data->recordLen) - (2*sizeof(uint8_t)); } private: @@ -369,7 +369,7 @@ namespace pcpp if (data == nullptr) return IPV4OPT_Unknown; - return (IPv4OptionTypes)data->recordType; + return static_cast(data->recordType); } }; @@ -637,7 +637,7 @@ namespace pcpp /** * @return Size of IPv4 header (including IPv4 options if exist) */ - size_t getHeaderLen() const { return (size_t)((uint16_t)(getIPv4Header()->internetHeaderLength) * 4) + m_TempHeaderExtension; } + size_t getHeaderLen() const { return static_cast(static_cast(getIPv4Header()->internetHeaderLength) * 4) + m_TempHeaderExtension; } /** * Calculate the following fields: From 9f33ead9e4e22e0f0f33eec8b26a65639f0f7664 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 3 Jun 2024 22:36:26 +0300 Subject: [PATCH 5/7] C cast -> Cpp cast --- Packet++/header/IPv6Layer.h | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/Packet++/header/IPv6Layer.h b/Packet++/header/IPv6Layer.h index 584dbcae76..f603083d59 100644 --- a/Packet++/header/IPv6Layer.h +++ b/Packet++/header/IPv6Layer.h @@ -231,19 +231,19 @@ namespace pcpp while (curExt != NULL && dynamic_cast(curExt) == NULL) curExt = curExt->getNextHeader(); - return (TIPv6Extension*)curExt; + return static_cast(curExt); } template TIPv6Extension* IPv6Layer::addExtension(const TIPv6Extension& extensionHeader) { - int offsetToAddHeader = (int)getHeaderLen(); + int offsetToAddHeader = static_cast(getHeaderLen()); if (!extendLayer(offsetToAddHeader, extensionHeader.getExtensionLen())) { return NULL; } - TIPv6Extension* newHeader = new TIPv6Extension(this, (size_t)offsetToAddHeader); + TIPv6Extension* newHeader = new TIPv6Extension(this, static_cast(offsetToAddHeader)); (*newHeader) = extensionHeader; if (m_FirstExtension != NULL) From 745bc7fddabab2f91f86d4cabe7f4305d49b95d2 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Mon, 3 Jun 2024 22:36:53 +0300 Subject: [PATCH 6/7] fixed cppcheck suggestion --- Packet++/src/IPv6Layer.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Packet++/src/IPv6Layer.cpp b/Packet++/src/IPv6Layer.cpp index 72d97657a8..7b6d63c2e8 100644 --- a/Packet++/src/IPv6Layer.cpp +++ b/Packet++/src/IPv6Layer.cpp @@ -187,7 +187,7 @@ void IPv6Layer::removeAllExtensions() bool IPv6Layer::isFragment() const { - IPv6FragmentationHeader* fragHdr = getExtensionOfType(); + IPv6FragmentationHeader const* fragHdr = getExtensionOfType(); return (fragHdr != nullptr); } From 94f6a171f3f175137267307d0be05c6f45be9219 Mon Sep 17 00:00:00 2001 From: Dimitar Krastev Date: Tue, 4 Jun 2024 02:15:56 +0300 Subject: [PATCH 7/7] Simplified nullptr check. --- Packet++/src/IPv6Layer.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/Packet++/src/IPv6Layer.cpp b/Packet++/src/IPv6Layer.cpp index 7b6d63c2e8..f6b5d78546 100644 --- a/Packet++/src/IPv6Layer.cpp +++ b/Packet++/src/IPv6Layer.cpp @@ -187,8 +187,7 @@ void IPv6Layer::removeAllExtensions() bool IPv6Layer::isFragment() const { - IPv6FragmentationHeader const* fragHdr = getExtensionOfType(); - return (fragHdr != nullptr); + return getExtensionOfType() != nullptr; } void IPv6Layer::parseNextLayer()