Skip to content

Commit

Permalink
Cpp casts and const changes. (#1428)
Browse files Browse the repository at this point in the history
* Fixed uninitialized variable.

* Marked function as const.

* Fixed potential overflow warning.

* Replaced C-style casts -> Cpp casts.

* C cast -> Cpp cast

* fixed cppcheck suggestion

* Simplified nullptr check.
  • Loading branch information
Dimi1010 authored Jun 4, 2024
1 parent 817b0f0 commit 001b2bc
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 25 deletions.
32 changes: 16 additions & 16 deletions Packet++/header/IPv4Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,9 @@ namespace pcpp
if (dataSize < 2)
return res;

uint8_t valueOffset = (uint8_t)(1);
uint8_t valueOffset = static_cast<uint8_t>(1);

while ((size_t)valueOffset < dataSize)
while (static_cast<size_t>(valueOffset) < dataSize)
{
uint32_t curValue;
memcpy(&curValue, m_Data->recordValue + valueOffset, sizeof(uint32_t));
Expand All @@ -253,7 +253,7 @@ namespace pcpp

res.push_back(IPv4Address(curValue));

valueOffset += (uint8_t)(4);
valueOffset += static_cast<uint8_t>(4);
}

return res;
Expand Down Expand Up @@ -281,12 +281,12 @@ namespace pcpp
if (dataSize < 2)
return res;

res.type = (IPv4TimestampOptionValue::TimestampType)m_Data->recordValue[1];
res.type = static_cast<IPv4TimestampOptionValue::TimestampType>(m_Data->recordValue[1]);

uint8_t valueOffset = (uint8_t)(2);
uint8_t valueOffset = static_cast<uint8_t>(2);
bool readIPAddr = (res.type == IPv4TimestampOptionValue::TimestampAndIP);

while ((size_t)valueOffset < dataSize)
while (static_cast<size_t>(valueOffset) < dataSize)
{
uint32_t curValue;
memcpy(&curValue, m_Data->recordValue + valueOffset, sizeof(uint32_t));
Expand All @@ -301,7 +301,7 @@ namespace pcpp
if (res.type == IPv4TimestampOptionValue::TimestampAndIP)
readIPAddr = !readIPAddr;

valueOffset += (uint8_t)(4);
valueOffset += static_cast<uint8_t>(4);
}

return res;
Expand All @@ -323,14 +323,14 @@ namespace pcpp
*/
static bool canAssign(const uint8_t* recordRawData, size_t tlvDataLen)
{
auto data = (TLVRawData*)recordRawData;
auto data = reinterpret_cast<TLVRawData const*>(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<uint8_t>(IPV4OPT_EndOfOptionsList) || data->recordType == static_cast<uint8_t>(IPV4OPT_NOP))
return true;

return TLVRecord<uint8_t, uint8_t>::canAssign(recordRawData, tlvDataLen);
Expand All @@ -343,21 +343,21 @@ 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<uint8_t>(IPV4OPT_EndOfOptionsList) || m_Data->recordType == static_cast<uint8_t>(IPV4OPT_NOP))
return sizeof(uint8_t);

return (size_t)m_Data->recordLen;
return static_cast<size_t>(m_Data->recordLen);
}

size_t getDataSize() const
{
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<uint8_t>(IPV4OPT_EndOfOptionsList) || m_Data->recordType == static_cast<uint8_t>(IPV4OPT_NOP))
return 0;

return (size_t)m_Data->recordLen - (2*sizeof(uint8_t));
return static_cast<size_t>(m_Data->recordLen) - (2*sizeof(uint8_t));
}

private:
Expand All @@ -369,7 +369,7 @@ namespace pcpp
if (data == nullptr)
return IPV4OPT_Unknown;

return (IPv4OptionTypes)data->recordType;
return static_cast<IPv4OptionTypes>(data->recordType);
}
};

Expand Down Expand Up @@ -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<size_t>(static_cast<uint16_t>(getIPv4Header()->internetHeaderLength) * 4) + m_TempHeaderExtension; }

/**
* Calculate the following fields:
Expand Down
6 changes: 3 additions & 3 deletions Packet++/header/IPv6Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -231,19 +231,19 @@ namespace pcpp
while (curExt != NULL && dynamic_cast<TIPv6Extension*>(curExt) == NULL)
curExt = curExt->getNextHeader();

return (TIPv6Extension*)curExt;
return static_cast<TIPv6Extension*>(curExt);
}

template<class TIPv6Extension>
TIPv6Extension* IPv6Layer::addExtension(const TIPv6Extension& extensionHeader)
{
int offsetToAddHeader = (int)getHeaderLen();
int offsetToAddHeader = static_cast<int>(getHeaderLen());
if (!extendLayer(offsetToAddHeader, extensionHeader.getExtensionLen()))
{
return NULL;
}

TIPv6Extension* newHeader = new TIPv6Extension(this, (size_t)offsetToAddHeader);
TIPv6Extension* newHeader = new TIPv6Extension(this, static_cast<size_t>(offsetToAddHeader));
(*newHeader) = extensionHeader;

if (m_FirstExtension != NULL)
Expand Down
3 changes: 1 addition & 2 deletions Packet++/src/IPv6Layer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,7 @@ void IPv6Layer::removeAllExtensions()

bool IPv6Layer::isFragment() const
{
IPv6FragmentationHeader* fragHdr = getExtensionOfType<IPv6FragmentationHeader>();
return (fragHdr != nullptr);
return getExtensionOfType<IPv6FragmentationHeader>() != nullptr;
}

void IPv6Layer::parseNextLayer()
Expand Down
2 changes: 1 addition & 1 deletion Pcap++/header/PcapLiveDevice.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions Pcap++/src/PcapLiveDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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<int>(m_DeviceMtu))
{
Expand Down
2 changes: 1 addition & 1 deletion Pcap++/src/PcapRemoteDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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<uint64_t>(tempStats->ps_drop) + tempStats->ps_netdrop;
stats.packetsDropByInterface = tempStats->ps_ifdrop;
}

Expand Down

0 comments on commit 001b2bc

Please sign in to comment.