Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Clang tidy fixes for Packet++ #1676

Draft
wants to merge 6 commits into
base: dev
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Checks: 'cert-*,
clang-analyzer-*,
concurrency-*,
cppcoreguidelines-*,
misc-*,
modernize-*,
performance-*,
portability-*,
readability-*,
-cert-env33-c,
-cert-err58-cpp,
-clang-analyzer-optin.cplusplus.VirtualCall,
-cppcoreguidelines-avoid-c-arrays,
-cppcoreguidelines-avoid-do-while,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-avoid-non-const-global-variables,
-cppcoreguidelines-macro-usage,
-cppcoreguidelines-owning-memory,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
-cppcoreguidelines-pro-bounds-constant-array-index,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
-cppcoreguidelines-pro-type-reinterpret-cast,
-cppcoreguidelines-pro-type-const-cast,
-cppcoreguidelines-pro-type-vararg,
-cppcoreguidelines-special-member-functions,
-modernize-avoid-c-arrays,
-modernize-use-trailing-return-type,
-misc-header-include-cycle,
-misc-include-cleaner,
-misc-no-recursion,
-misc-non-private-member-variables-in-classes,
-misc-use-anonymous-namespace,
-readability-function-cognitive-complexity,
-readability-magic-numbers'
52 changes: 25 additions & 27 deletions Packet++/header/BgpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ namespace pcpp
* @struct bgp_open_message
* BGP OPEN message structure
*/
typedef struct bgp_open_message : bgp_common_header
using bgp_open_message = struct bgp_open_message : bgp_common_header
{
/** BGP version number */
uint8_t version;
Expand All @@ -162,7 +162,7 @@ namespace pcpp
uint32_t bgpId;
/** The total length of the Optional Parameters field */
uint8_t optionalParameterLength;
} bgp_open_message;
};
#pragma pack(pop)

/**
Expand All @@ -172,17 +172,16 @@ namespace pcpp
struct optional_parameter
{
/** Parameter type */
uint8_t type;
uint8_t type{};
/** Parameter length */
uint8_t length;
uint8_t length{};
/** Parameter data */
uint8_t value[32];
uint8_t value[32]{};

/**
* A default c'tor that zeroes all data
*/
optional_parameter()
{}
optional_parameter() = default;

/**
* A c'tor that initializes the values of the struct
Expand Down Expand Up @@ -237,7 +236,7 @@ namespace pcpp
* Set the BGP identifier
* @param[in] newBgpId BGP identifier to set. If value is not a valid IPv4 address it won't be set
*/
void setBgpId(const IPv4Address& newBgpId);
void setBgpId(const IPv4Address& newBgpId) const;

/**
* Get a vector of the optional parameters in the message
Expand All @@ -249,7 +248,7 @@ namespace pcpp
/**
* @return The length in [bytes] of the optional parameters data in the message
*/
size_t getOptionalParametersLength();
size_t getOptionalParametersLength() const;

/**
* Set optional parameters in the message. This method will override all existing optional parameters currently
Expand Down Expand Up @@ -278,8 +277,8 @@ namespace pcpp
}

private:
size_t optionalParamsToByteArray(const std::vector<optional_parameter>& optionalParams, uint8_t* resultByteArr,
size_t maxByteArrSize);
static size_t optionalParamsToByteArray(const std::vector<optional_parameter>& optionalParams,
uint8_t* resultByteArr, size_t maxByteArrSize);
};

/**
Expand Down Expand Up @@ -323,19 +322,18 @@ namespace pcpp
struct path_attribute
{
/** Path attribute flags */
uint8_t flags;
uint8_t flags{};
/** Path attribute type */
uint8_t type;
uint8_t type{};
/** Path attribute length */
uint8_t length;
uint8_t length{};
/** Path attribute data. Max supported data length is 32 bytes */
uint8_t data[32];
uint8_t data[32]{};

/**
* A default c'tor that zeroes all data
*/
path_attribute()
{}
path_attribute() = default;

/**
* A c'tor that initializes the values of the struct
Expand Down Expand Up @@ -486,13 +484,13 @@ namespace pcpp
}

private:
void parsePrefixAndIPData(uint8_t* dataPtr, size_t dataLen, std::vector<prefix_and_ip>& result);
static void parsePrefixAndIPData(uint8_t* dataPtr, size_t dataLen, std::vector<prefix_and_ip>& result);

size_t prefixAndIPDataToByteArray(const std::vector<prefix_and_ip>& prefixAndIpData, uint8_t* resultByteArr,
size_t maxByteArrSize);
static size_t prefixAndIPDataToByteArray(const std::vector<prefix_and_ip>& prefixAndIpData,
uint8_t* resultByteArr, size_t maxByteArrSize);

size_t pathAttributesToByteArray(const std::vector<path_attribute>& pathAttributes, uint8_t* resultByteArr,
size_t maxByteArrSize);
static size_t pathAttributesToByteArray(const std::vector<path_attribute>& pathAttributes,
uint8_t* resultByteArr, size_t maxByteArrSize);
};

/**
Expand All @@ -507,13 +505,13 @@ namespace pcpp
* @struct bgp_notification_message
* BGP NOTIFICATION message structure
*/
typedef struct bgp_notification_message : bgp_common_header
using bgp_notification_message = struct bgp_notification_message : bgp_common_header
{
/** BGP notification error code */
uint8_t errorCode;
/** BGP notification error sub-code */
uint8_t errorSubCode;
} bgp_notification_message;
};
#pragma pack(pop)

/**
Expand Down Expand Up @@ -627,7 +625,7 @@ namespace pcpp
* @typedef bgp_keepalive_message
* BGP KEEPALIVE message structure
*/
typedef bgp_common_header bgp_keepalive_message;
using bgp_keepalive_message = bgp_common_header;

/**
* A constructor that creates the layer from an existing packet raw data
Expand Down Expand Up @@ -675,15 +673,15 @@ namespace pcpp
* @struct bgp_route_refresh_message
* BGP ROUTE-REFRESH message structure
*/
typedef struct bgp_route_refresh_message : bgp_common_header
using bgp_route_refresh_message = struct bgp_route_refresh_message : bgp_common_header
{
/** Address Family Identifier */
uint16_t afi;
/** Reserved field */
uint8_t reserved;
/** Subsequent Address Family Identifier */
uint8_t safi;
} bgp_route_refresh_message;
};
#pragma pack(pop)

/**
Expand Down
4 changes: 2 additions & 2 deletions Packet++/header/CotpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,15 @@ namespace pcpp
* Represents a COTP protocol header
*/
#pragma pack(push, 1)
typedef struct
using cotphdr = struct cotphdr
{
/** length */
uint8_t length;
/** PDU type identifier */
uint8_t pduType;
/** TPDU number sequence*/
uint8_t tpduNumber;
} cotphdr;
};
#pragma pack(pop)

/**
Expand Down
41 changes: 27 additions & 14 deletions Packet++/header/DhcpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
#include "TLVData.h"
#include "IpAddress.h"
#include "MacAddress.h"
#include <string.h>
#include <cstring>

/// @file

Expand Down Expand Up @@ -441,7 +441,9 @@ namespace pcpp
std::string getValueAsString(int valueOffset = 0) const
{
if (m_Data == nullptr || m_Data->recordLen - valueOffset < 1)
{
return "";
}

return std::string(reinterpret_cast<const char*>(m_Data->recordValue) + valueOffset,
static_cast<int>(m_Data->recordLen) - valueOffset);
Expand All @@ -462,7 +464,9 @@ namespace pcpp

// use the length of input string if a buffer is large enough for whole string
if (stringValue.length() < len)
{
len = stringValue.length();
}

memcpy(m_Data->recordValue + valueOffset, stringValue.data(), len);
}
Expand All @@ -475,16 +479,22 @@ namespace pcpp
*/
static bool canAssign(const uint8_t* recordRawData, size_t tlvDataLen)
{
auto data = reinterpret_cast<TLVRawData const*>(recordRawData);
const auto* data = reinterpret_cast<TLVRawData const*>(recordRawData);
if (data == nullptr)
{
return false;
}

if (tlvDataLen < sizeof(TLVRawData::recordType))
{
return false;
}

if (data->recordType == static_cast<uint8_t>(DHCPOPT_END) ||
data->recordType == static_cast<uint8_t>(DHCPOPT_PAD))
{
return true;
}

return TLVRecord<uint8_t, uint8_t>::canAssign(recordRawData, tlvDataLen);
}
Expand All @@ -494,23 +504,31 @@ namespace pcpp
size_t getTotalSize() const override
{
if (m_Data == nullptr)
{
return 0;
}

if (m_Data->recordType == static_cast<uint8_t>(DHCPOPT_END) ||
m_Data->recordType == static_cast<uint8_t>(DHCPOPT_PAD))
{
return sizeof(uint8_t);
}

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

size_t getDataSize() const override
{
if (m_Data == nullptr)
{
return 0;
}

if (m_Data->recordType == static_cast<uint8_t>(DHCPOPT_END) ||
m_Data->recordType == static_cast<uint8_t>(DHCPOPT_PAD))
{
return 0;
}

return m_Data->recordLen;
}
Expand Down Expand Up @@ -590,19 +608,14 @@ namespace pcpp
* A copy c'tor which copies all the data from another instance of DhcpOptionBuilder
* @param[in] other The instance to copy from
*/
DhcpOptionBuilder(const DhcpOptionBuilder& other) : TLVRecordBuilder(other)
{}
DhcpOptionBuilder(const DhcpOptionBuilder& other) = default;

/**
* Assignment operator that copies all data from another instance of DhcpOptionBuilder
* @param[in] other The instance to assign from
* @return A reference to the assignee
*/
DhcpOptionBuilder& operator=(const DhcpOptionBuilder& other)
{
TLVRecordBuilder::operator=(other);
return *this;
}
DhcpOptionBuilder& operator=(const DhcpOptionBuilder& other) = default;

/**
* Build the DhcpOption object out of the parameters defined in the c'tor
Expand Down Expand Up @@ -676,7 +689,7 @@ namespace pcpp
* Set the client IPv4 address in dhcp_header#clientIpAddress
* @param[in] addr The IPv4 address to set
*/
void setClientIpAddress(const IPv4Address& addr)
void setClientIpAddress(const IPv4Address& addr) const
{
getDhcpHeader()->clientIpAddress = addr.toInt();
}
Expand All @@ -694,7 +707,7 @@ namespace pcpp
* Set the server IPv4 address in dhcp_header#serverIpAddress
* @param[in] addr The IPv4 address to set
*/
void setServerIpAddress(const IPv4Address& addr)
void setServerIpAddress(const IPv4Address& addr) const
{
getDhcpHeader()->serverIpAddress = addr.toInt();
}
Expand All @@ -711,7 +724,7 @@ namespace pcpp
* Set your IPv4 address in dhcp_header#yourIpAddress
* @param[in] addr The IPv4 address to set
*/
void setYourIpAddress(const IPv4Address& addr)
void setYourIpAddress(const IPv4Address& addr) const
{
getDhcpHeader()->yourIpAddress = addr.toInt();
}
Expand All @@ -728,7 +741,7 @@ namespace pcpp
* Set the gateway IPv4 address in dhcp_header#gatewayIpAddress
* @param[in] addr The IPv4 address to set
*/
void setGatewayIpAddress(const IPv4Address& addr)
void setGatewayIpAddress(const IPv4Address& addr) const
{
getDhcpHeader()->gatewayIpAddress = addr.toInt();
}
Expand All @@ -745,7 +758,7 @@ namespace pcpp
* dhcp_header#hardwareType to 1 (Ethernet) and dhcp_header#hardwareAddressLength to 6 (MAC address length)
* @param[in] addr The MAC address to set
*/
void setClientHardwareAddress(const MacAddress& addr);
void setClientHardwareAddress(const MacAddress& addr) const;

/**
* @return DHCP message type as extracted from ::DHCPOPT_DHCP_MESSAGE_TYPE option. If this option doesn't exist
Expand Down
2 changes: 1 addition & 1 deletion Packet++/header/DhcpV6Layer.h
Original file line number Diff line number Diff line change
Expand Up @@ -461,7 +461,7 @@ namespace pcpp

bool DhcpV6Layer::isDataValid(const uint8_t* data, size_t dataLen)
{
return data && dataLen >= sizeof(dhcpv6_header);
return (data != nullptr) && dataLen >= sizeof(dhcpv6_header);
}

} // namespace pcpp
Loading