From 9789273c4ca41f8638406b79444dd92810ba76e1 Mon Sep 17 00:00:00 2001 From: lumiZGorlic <45258126+lumiZGorlic@users.noreply.github.com> Date: Tue, 8 Oct 2024 09:52:26 +0200 Subject: [PATCH] C++ casting and some fixes (#1610) --- Common++/src/GeneralUtils.cpp | 9 +++--- Packet++/header/PacketTrailerLayer.h | 2 +- Packet++/src/CotpLayer.cpp | 2 +- Packet++/src/EthDot3Layer.cpp | 4 +-- Packet++/src/EthLayer.cpp | 4 +-- Packet++/src/NullLoopbackLayer.cpp | 4 +-- Packet++/src/PPPoELayer.cpp | 6 ++-- Packet++/src/SipLayer.cpp | 48 +++++++++++++++------------- Packet++/src/TLVData.cpp | 8 ++--- Packet++/src/TextBasedProtocol.cpp | 40 ++++++++++++----------- 10 files changed, 67 insertions(+), 60 deletions(-) diff --git a/Common++/src/GeneralUtils.cpp b/Common++/src/GeneralUtils.cpp index 7e9bb6d452..04aec21a62 100644 --- a/Common++/src/GeneralUtils.cpp +++ b/Common++/src/GeneralUtils.cpp @@ -18,10 +18,10 @@ namespace pcpp dataStream << std::hex; for (size_t i = 0; i < byteArrSize; ++i) { - if (i >= (size_t)stringSizeLimit) + if (i >= static_cast(stringSizeLimit)) break; - dataStream << std::setw(2) << std::setfill('0') << (int)byteArr[i]; + dataStream << std::setw(2) << std::setfill('0') << static_cast(byteArr[i]); } return dataStream.str(); @@ -69,10 +69,11 @@ namespace pcpp char* cross_platform_memmem(const char* haystack, size_t haystackLen, const char* needle, size_t needleLen) { - char* ptr = (char*)haystack; + char* ptr = const_cast(haystack); while (needleLen <= (haystackLen - (ptr - haystack))) { - if (nullptr != (ptr = (char*)memchr(ptr, (int)(*needle), haystackLen - (ptr - haystack)))) + if (nullptr != + (ptr = static_cast(memchr(ptr, static_cast(*needle), haystackLen - (ptr - haystack))))) { // check if there is room to do a memcmp if (needleLen > (haystackLen - (ptr - haystack))) diff --git a/Packet++/header/PacketTrailerLayer.h b/Packet++/header/PacketTrailerLayer.h index 49b24fbd50..bf8d056074 100644 --- a/Packet++/header/PacketTrailerLayer.h +++ b/Packet++/header/PacketTrailerLayer.h @@ -8,7 +8,7 @@ namespace pcpp { /** * @class PacketTrailerLayer - * A class for representing packet tailer (a.k.a footer or padding) which refers to supplemental data placed at the + * A class for representing packet trailer (a.k.a footer or padding) which refers to supplemental data placed at the * end of a block of data being stored or transmitted, which may contain information for the handling of the data * block, or just mark its end (taken from Wikipedia: https://en.wikipedia.org/wiki/Trailer_(computing) ) * diff --git a/Packet++/src/CotpLayer.cpp b/Packet++/src/CotpLayer.cpp index 71a769d9a0..2a218f547a 100644 --- a/Packet++/src/CotpLayer.cpp +++ b/Packet++/src/CotpLayer.cpp @@ -11,7 +11,7 @@ namespace pcpp m_DataLen = headerLen; m_Data = new uint8_t[headerLen]; memset(m_Data, 0, headerLen); - cotphdr* cotpHdr = (cotphdr*)m_Data; + cotphdr* cotpHdr = getCotpHeader(); cotpHdr->length = 0x02; cotpHdr->pduType = 0x0f; cotpHdr->tpduNumber = tpduNumber; diff --git a/Packet++/src/EthDot3Layer.cpp b/Packet++/src/EthDot3Layer.cpp index dff65ac12a..7a97897337 100644 --- a/Packet++/src/EthDot3Layer.cpp +++ b/Packet++/src/EthDot3Layer.cpp @@ -13,7 +13,7 @@ namespace pcpp m_Data = new uint8_t[headerLen]; memset(m_Data, 0, headerLen); - ether_dot3_header* ethHdr = (ether_dot3_header*)m_Data; + ether_dot3_header* ethHdr = getEthHeader(); destMac.copyTo(ethHdr->dstMac); sourceMac.copyTo(ethHdr->srcMac); ethHdr->length = be16toh(length); @@ -51,7 +51,7 @@ namespace pcpp * From: https://tools.ietf.org/html/rfc5342#section-2.3.2.1 * More: IEEE Std 802.3 Clause 3.2.6 */ - return be16toh(*(uint16_t*)(data + 12)) <= (uint16_t)0x05DC; + return be16toh(*reinterpret_cast(data + 12)) <= static_cast(0x05DC); } else { diff --git a/Packet++/src/EthLayer.cpp b/Packet++/src/EthLayer.cpp index c607076092..611bc10138 100644 --- a/Packet++/src/EthLayer.cpp +++ b/Packet++/src/EthLayer.cpp @@ -21,7 +21,7 @@ namespace pcpp m_Data = new uint8_t[headerLen]; memset(m_Data, 0, headerLen); - ether_header* ethHdr = (ether_header*)m_Data; + ether_header* ethHdr = getEthHeader(); destMac.copyTo(ethHdr->dstMac); sourceMac.copyTo(ethHdr->srcMac); ethHdr->etherType = htobe16(etherType); @@ -121,7 +121,7 @@ namespace pcpp * From: https://tools.ietf.org/html/rfc5342#section-2.3.2.1 * More: IEEE Std 802.3 Clause 3.2.6 */ - return be16toh(*(uint16_t*)(data + 12)) >= (uint16_t)0x0600; + return be16toh(*reinterpret_cast(data + 12)) >= static_cast(0x0600); } else { diff --git a/Packet++/src/NullLoopbackLayer.cpp b/Packet++/src/NullLoopbackLayer.cpp index fa9f4951ba..2717d8ff6c 100644 --- a/Packet++/src/NullLoopbackLayer.cpp +++ b/Packet++/src/NullLoopbackLayer.cpp @@ -25,7 +25,7 @@ namespace pcpp uint32_t NullLoopbackLayer::getFamily() const { - uint32_t family = *(uint32_t*)m_Data; + uint32_t family = *(reinterpret_cast(m_Data)); if ((family & 0xFFFF0000) != 0) { if ((family & 0xFF000000) == 0 && (family & 0x00FF0000) < 0x00060000) @@ -58,7 +58,7 @@ namespace pcpp uint32_t family = getFamily(); if (family > IEEE_802_3_MAX_LEN) { - uint16_t ethType = (uint16_t)family; + uint16_t ethType = static_cast(family); switch (ethType) { case PCPP_ETHERTYPE_IP: diff --git a/Packet++/src/PPPoELayer.cpp b/Packet++/src/PPPoELayer.cpp index 25b07f4033..c30625cfd9 100644 --- a/Packet++/src/PPPoELayer.cpp +++ b/Packet++/src/PPPoELayer.cpp @@ -33,7 +33,7 @@ namespace pcpp void PPPoELayer::computeCalculateFields() { - pppoe_header* pppoeHdr = (pppoe_header*)m_Data; + pppoe_header* pppoeHdr = getPPPoEHeader(); pppoeHdr->payloadLength = htobe16(m_DataLen - sizeof(pppoe_header)); } @@ -75,7 +75,7 @@ namespace pcpp return 0; } - uint16_t pppNextProto = *(uint16_t*)(m_Data + sizeof(pppoe_header)); + uint16_t pppNextProto = *reinterpret_cast(m_Data + sizeof(pppoe_header)); return be16toh(pppNextProto); } @@ -87,7 +87,7 @@ namespace pcpp return; } - uint16_t* pppProto = (uint16_t*)(m_Data + sizeof(pppoe_header)); + uint16_t* pppProto = reinterpret_cast(m_Data + sizeof(pppoe_header)); *pppProto = htobe16(nextProtocol); } diff --git a/Packet++/src/SipLayer.cpp b/Packet++/src/SipLayer.cpp index 5872c7b1bb..ac9acb1289 100644 --- a/Packet++/src/SipLayer.cpp +++ b/Packet++/src/SipLayer.cpp @@ -98,7 +98,7 @@ namespace pcpp if (m_DataLen > headerLen) { int currentContentLength = getContentLength(); - if (currentContentLength != (int)(m_DataLen - headerLen)) + if (currentContentLength != static_cast(m_DataLen - headerLen)) setContentLength(m_DataLen - headerLen); } } @@ -107,7 +107,7 @@ namespace pcpp SipRequestFirstLine::SipRequestFirstLine(SipRequestLayer* sipRequest) : m_SipRequest(sipRequest) { - m_Method = parseMethod((char*)m_SipRequest->m_Data, m_SipRequest->getDataLen()); + m_Method = parseMethod(reinterpret_cast(m_SipRequest->m_Data), m_SipRequest->getDataLen()); if (m_Method == SipRequestLayer::SipMethodUnknown) { m_UriOffset = -1; @@ -119,10 +119,11 @@ namespace pcpp parseVersion(); char* endOfFirstLine; - if ((endOfFirstLine = (char*)memchr((char*)(m_SipRequest->m_Data + m_VersionOffset), '\n', - m_SipRequest->m_DataLen - (size_t)m_VersionOffset)) != nullptr) + if ((endOfFirstLine = + static_cast(memchr(reinterpret_cast(m_SipRequest->m_Data + m_VersionOffset), '\n', + m_SipRequest->m_DataLen - static_cast(m_VersionOffset)))) != nullptr) { - m_FirstLineEndOffset = endOfFirstLine - (char*)m_SipRequest->m_Data + 1; + m_FirstLineEndOffset = endOfFirstLine - reinterpret_cast(m_SipRequest->m_Data) + 1; m_IsComplete = true; } else @@ -216,8 +217,8 @@ namespace pcpp return; } - char* data = (char*)(m_SipRequest->m_Data + m_UriOffset); - char* verPos = (char*)cross_platform_memmem(data, m_SipRequest->getDataLen() - m_UriOffset, " SIP/", 5); + char* data = reinterpret_cast(m_SipRequest->m_Data + m_UriOffset); + char* verPos = cross_platform_memmem(data, m_SipRequest->getDataLen() - m_UriOffset, " SIP/", 5); if (verPos == nullptr) { m_Version = ""; @@ -226,7 +227,8 @@ namespace pcpp } // verify packet doesn't end before the version, meaning still left place for " SIP/x.y" (7 chars) - if ((uint16_t)(verPos + 7 - (char*)m_SipRequest->m_Data) > m_SipRequest->getDataLen()) + if (static_cast(verPos + 7 - reinterpret_cast(m_SipRequest->m_Data)) > + m_SipRequest->getDataLen()) { m_Version = ""; m_VersionOffset = -1; @@ -237,13 +239,13 @@ namespace pcpp verPos++; int endOfVerPos = 0; - while (((verPos + endOfVerPos) < (char*)(m_SipRequest->m_Data + m_SipRequest->m_DataLen)) && + while (((verPos + endOfVerPos) < reinterpret_cast(m_SipRequest->m_Data + m_SipRequest->m_DataLen)) && ((verPos + endOfVerPos)[0] != '\r') && ((verPos + endOfVerPos)[0] != '\n')) endOfVerPos++; m_Version = std::string(verPos, endOfVerPos); - m_VersionOffset = verPos - (char*)m_SipRequest->m_Data; + m_VersionOffset = verPos - reinterpret_cast(m_SipRequest->m_Data); } bool SipRequestFirstLine::setMethod(SipRequestLayer::SipMethod newMethod) @@ -295,7 +297,8 @@ namespace pcpp { std::string result; if (m_UriOffset != -1 && m_VersionOffset != -1) - result.assign((char*)(m_SipRequest->m_Data + m_UriOffset), m_VersionOffset - 1 - m_UriOffset); + result.assign(reinterpret_cast(m_SipRequest->m_Data + m_UriOffset), + m_VersionOffset - 1 - m_UriOffset); // else first line is illegal, return empty string @@ -396,7 +399,7 @@ namespace pcpp if (size <= maxLengthToPrint) { char* firstLine = new char[size + 1]; - strncpy(firstLine, (char*)m_Data, size); + strncpy(firstLine, reinterpret_cast(m_Data), size); firstLine[size] = 0; result += std::string(firstLine); delete[] firstLine; @@ -404,7 +407,7 @@ namespace pcpp else { char firstLine[maxLengthToPrint + 1]; - strncpy(firstLine, (char*)m_Data, maxLengthToPrint - 3); + strncpy(firstLine, reinterpret_cast(m_Data), maxLengthToPrint - 3); firstLine[maxLengthToPrint - 3] = '.'; firstLine[maxLengthToPrint - 2] = '.'; firstLine[maxLengthToPrint - 1] = '.'; @@ -633,7 +636,7 @@ namespace pcpp if (size <= maxLengthToPrint) { char* firstLine = new char[size + 1]; - strncpy(firstLine, (char*)m_Data, size); + strncpy(firstLine, reinterpret_cast(m_Data), size); firstLine[size] = 0; result += std::string(firstLine); delete[] firstLine; @@ -641,7 +644,7 @@ namespace pcpp else { char firstLine[maxLengthToPrint + 1]; - strncpy(firstLine, (char*)m_Data, maxLengthToPrint - 3); + strncpy(firstLine, reinterpret_cast(m_Data), maxLengthToPrint - 3); firstLine[maxLengthToPrint - 3] = '.'; firstLine[maxLengthToPrint - 2] = '.'; firstLine[maxLengthToPrint - 1] = '.'; @@ -668,7 +671,7 @@ namespace pcpp int statusStringEndOffset = m_FirstLineEndOffset - 2; if ((*(m_SipResponse->m_Data + statusStringEndOffset)) != '\r') statusStringEndOffset++; - result.assign((char*)(m_SipResponse->m_Data + statusStringOffset), + result.assign(reinterpret_cast(m_SipResponse->m_Data + statusStringOffset), statusStringEndOffset - statusStringOffset); } @@ -742,7 +745,7 @@ namespace pcpp return; } - char* verPos = (char*)m_SipResponse->m_Data; + char* verPos = reinterpret_cast(m_SipResponse->m_Data); memcpy(verPos, newVersion.c_str(), newVersion.length()); m_Version = newVersion; } @@ -771,20 +774,21 @@ namespace pcpp SipResponseFirstLine::SipResponseFirstLine(SipResponseLayer* sipResponse) : m_SipResponse(sipResponse) { - m_Version = parseVersion((char*)m_SipResponse->m_Data, m_SipResponse->getDataLen()); + m_Version = parseVersion(reinterpret_cast(m_SipResponse->m_Data), m_SipResponse->getDataLen()); if (m_Version == "") { m_StatusCode = SipResponseLayer::SipStatusCodeUnknown; } else { - m_StatusCode = parseStatusCode((char*)m_SipResponse->m_Data, m_SipResponse->getDataLen()); + m_StatusCode = parseStatusCode(reinterpret_cast(m_SipResponse->m_Data), m_SipResponse->getDataLen()); } char* endOfFirstLine; - if ((endOfFirstLine = (char*)memchr((char*)(m_SipResponse->m_Data), '\n', m_SipResponse->m_DataLen)) != nullptr) + if ((endOfFirstLine = static_cast( + memchr(reinterpret_cast(m_SipResponse->m_Data), '\n', m_SipResponse->m_DataLen))) != nullptr) { - m_FirstLineEndOffset = endOfFirstLine - (char*)m_SipResponse->m_Data + 1; + m_FirstLineEndOffset = endOfFirstLine - reinterpret_cast(m_SipResponse->m_Data) + 1; m_IsComplete = true; } else @@ -852,7 +856,7 @@ namespace pcpp return ""; } - char* nextSpace = (char*)memchr(data, ' ', dataLen); + const char* nextSpace = static_cast(memchr(data, ' ', dataLen)); if (nextSpace == nullptr) return ""; diff --git a/Packet++/src/TLVData.cpp b/Packet++/src/TLVData.cpp index 7fc571635d..48da747aa2 100644 --- a/Packet++/src/TLVData.cpp +++ b/Packet++/src/TLVData.cpp @@ -25,19 +25,19 @@ namespace pcpp TLVRecordBuilder::TLVRecordBuilder(uint32_t recType, uint16_t recValue) { recValue = htobe16(recValue); - init(recType, (uint8_t*)&recValue, sizeof(uint16_t)); + init(recType, reinterpret_cast(&recValue), sizeof(uint16_t)); } TLVRecordBuilder::TLVRecordBuilder(uint32_t recType, uint32_t recValue) { recValue = htobe32(recValue); - init(recType, (uint8_t*)&recValue, sizeof(uint32_t)); + init(recType, reinterpret_cast(&recValue), sizeof(uint32_t)); } TLVRecordBuilder::TLVRecordBuilder(uint32_t recType, const IPv4Address& recValue) { uint32_t recIntValue = recValue.toInt(); - init(recType, (uint8_t*)&recIntValue, sizeof(uint32_t)); + init(recType, reinterpret_cast(&recIntValue), sizeof(uint32_t)); } TLVRecordBuilder::TLVRecordBuilder(uint32_t recType, const std::string& recValue, bool valueIsHexString) @@ -57,7 +57,7 @@ namespace pcpp } else { - uint8_t* recValueByteArr = (uint8_t*)recValue.c_str(); + const uint8_t* recValueByteArr = reinterpret_cast(recValue.c_str()); init(recType, recValueByteArr, recValue.length()); } } diff --git a/Packet++/src/TextBasedProtocol.cpp b/Packet++/src/TextBasedProtocol.cpp index a5688c160c..1d35996384 100644 --- a/Packet++/src/TextBasedProtocol.cpp +++ b/Packet++/src/TextBasedProtocol.cpp @@ -430,12 +430,12 @@ namespace pcpp m_NameOffsetInMessage(offsetInMessage), m_NextField(nullptr), m_NameValueSeparator(nameValueSeparator), m_SpacesAllowedBetweenNameAndValue(spacesAllowedBetweenNameAndValue) { - char* fieldData = (char*)(m_TextBasedProtocolMessage->m_Data + m_NameOffsetInMessage); - char* fieldEndPtr = - (char*)memchr(fieldData, '\n', m_TextBasedProtocolMessage->m_DataLen - (size_t)m_NameOffsetInMessage); + char* fieldData = reinterpret_cast(m_TextBasedProtocolMessage->m_Data + m_NameOffsetInMessage); + char* fieldEndPtr = static_cast(memchr( + fieldData, '\n', m_TextBasedProtocolMessage->m_DataLen - static_cast(m_NameOffsetInMessage))); if (fieldEndPtr == nullptr) - m_FieldSize = - tbp_my_own_strnlen(fieldData, m_TextBasedProtocolMessage->m_DataLen - (size_t)m_NameOffsetInMessage); + m_FieldSize = tbp_my_own_strnlen(fieldData, m_TextBasedProtocolMessage->m_DataLen - + static_cast(m_NameOffsetInMessage)); else m_FieldSize = fieldEndPtr - fieldData + 1; @@ -451,8 +451,9 @@ namespace pcpp else m_IsEndOfHeaderField = false; - char* fieldValuePtr = (char*)memchr(fieldData, nameValueSeparator, - m_TextBasedProtocolMessage->m_DataLen - (size_t)m_NameOffsetInMessage); + char* fieldValuePtr = static_cast( + memchr(fieldData, nameValueSeparator, + m_TextBasedProtocolMessage->m_DataLen - static_cast(m_NameOffsetInMessage))); // could not find the position of the separator, meaning field value position is unknown if (fieldValuePtr == nullptr || (fieldEndPtr != nullptr && fieldValuePtr >= fieldEndPtr)) { @@ -468,7 +469,7 @@ namespace pcpp fieldValuePtr++; // reached the end of the packet and value start offset wasn't found - if ((size_t)(fieldValuePtr - (char*)(m_TextBasedProtocolMessage->m_Data)) >= + if (static_cast(fieldValuePtr - reinterpret_cast(m_TextBasedProtocolMessage->m_Data)) >= m_TextBasedProtocolMessage->getDataLen()) { m_ValueOffsetInMessage = -1; @@ -480,16 +481,17 @@ namespace pcpp { // advance fieldValuePtr 1 byte forward while didn't get to end of packet and fieldValuePtr points to a // space char - while ((size_t)(fieldValuePtr - (char*)m_TextBasedProtocolMessage->m_Data) < - m_TextBasedProtocolMessage->getDataLen() && - (*fieldValuePtr) == ' ') + while ( + static_cast(fieldValuePtr - reinterpret_cast(m_TextBasedProtocolMessage->m_Data)) < + m_TextBasedProtocolMessage->getDataLen() && + (*fieldValuePtr) == ' ') { fieldValuePtr++; } } // reached the end of the packet and value start offset wasn't found - if ((size_t)(fieldValuePtr - (char*)(m_TextBasedProtocolMessage->m_Data)) >= + if (static_cast(fieldValuePtr - reinterpret_cast(m_TextBasedProtocolMessage->m_Data)) >= m_TextBasedProtocolMessage->getDataLen()) { m_ValueOffsetInMessage = -1; @@ -497,13 +499,13 @@ namespace pcpp } else { - m_ValueOffsetInMessage = fieldValuePtr - (char*)m_TextBasedProtocolMessage->m_Data; + m_ValueOffsetInMessage = fieldValuePtr - reinterpret_cast(m_TextBasedProtocolMessage->m_Data); // couldn't find the end of the field, so assuming the field value length is from m_ValueOffsetInMessage // until the end of the packet if (fieldEndPtr == nullptr) { // clang-format off - m_FieldValueSize = (char*)(m_TextBasedProtocolMessage->m_Data + m_TextBasedProtocolMessage->getDataLen()) - fieldValuePtr; + m_FieldValueSize = reinterpret_cast(m_TextBasedProtocolMessage->m_Data + m_TextBasedProtocolMessage->getDataLen()) - fieldValuePtr; // clang-format on } else @@ -596,9 +598,9 @@ namespace pcpp char* HeaderField::getData() const { if (m_TextBasedProtocolMessage == nullptr) - return (char*)m_NewFieldData; + return reinterpret_cast(m_NewFieldData); else - return (char*)(m_TextBasedProtocolMessage->m_Data); + return reinterpret_cast(m_TextBasedProtocolMessage->m_Data); } void HeaderField::setNextField(HeaderField* nextField) @@ -615,8 +617,8 @@ namespace pcpp { std::string result; - if (m_FieldNameSize != (size_t)-1) - result.assign((const char*)(((HeaderField*)this)->getData() + m_NameOffsetInMessage), m_FieldNameSize); + if (m_FieldNameSize != static_cast(-1)) + result.assign((getData() + m_NameOffsetInMessage), m_FieldNameSize); return result; } @@ -625,7 +627,7 @@ namespace pcpp { std::string result; if (m_ValueOffsetInMessage != -1) - result.assign((const char*)(((HeaderField*)this)->getData() + m_ValueOffsetInMessage), m_FieldValueSize); + result.assign((getData() + m_ValueOffsetInMessage), m_FieldValueSize); return result; }