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 Common++ #1639

Merged
merged 29 commits into from
Jan 12, 2025
Merged
Show file tree
Hide file tree
Changes from 10 commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
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-new
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
Checks: 'cert-*,
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I used this configuration to fix issues. Since all of directories are not fixed this file not used for now in CI

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'
2 changes: 1 addition & 1 deletion Common++/header/GeneralUtils.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <string>
#include <stdint.h>
#include <cstdint>
#include <type_traits>

/// @file
Expand Down
108 changes: 48 additions & 60 deletions Common++/header/IpAddress.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#pragma once

#include <stdint.h>
#include <string.h>
#include <cstdint>
#include <cstring>
#include <string>
#include <algorithm>
#include <ostream>
Expand Down Expand Up @@ -145,7 +145,7 @@ namespace pcpp

uint32_t IPv4Address::toInt() const
{
uint32_t addr;
uint32_t addr = 0;
memcpy(&addr, m_Bytes.data(), m_Bytes.size() * sizeof(uint8_t));
return addr;
}
Expand Down Expand Up @@ -395,7 +395,9 @@ namespace pcpp
bool IPAddress::operator==(const IPAddress& rhs) const
{
if (isIPv4())
{
return rhs.isIPv4() ? (m_IPv4 == rhs.m_IPv4) : false;
}

return rhs.isIPv6() ? m_IPv6 == rhs.m_IPv6 : false;
}
Expand Down Expand Up @@ -433,7 +435,7 @@ namespace pcpp
/// A constructor that creates an instance of the class out of an address and a full prefix length,
/// essentially making a network of consisting of only 1 address.
/// @param address An address representing the network prefix.
explicit IPv4Network(const IPv4Address& address) : IPv4Network(address, 32u)
explicit IPv4Network(const IPv4Address& address) : IPv4Network(address, 32U)
{}

/// A constructor that creates an instance of the class out of an address representing the network prefix
Expand Down Expand Up @@ -476,7 +478,7 @@ namespace pcpp
/// @return The network prefix, for example: the network prefix of 10.10.10.10/16 is 10.10.0.0
IPv4Address getNetworkPrefix() const
{
return IPv4Address(m_NetworkPrefix);
return m_NetworkPrefix;
}

/// @return The lowest non-reserved IPv4 address in this network, for example: the lowest address
Expand Down Expand Up @@ -505,10 +507,10 @@ namespace pcpp
std::string toString() const;

private:
uint32_t m_NetworkPrefix;
uint32_t m_Mask;
uint32_t m_NetworkPrefix{};
uint32_t m_Mask{};
seladb marked this conversation as resolved.
Show resolved Hide resolved

bool isValidNetmask(const IPv4Address& netmaskAddress);
static bool isValidNetmask(const IPv4Address& netmaskAddress);
void initFromAddressAndPrefixLength(const IPv4Address& address, uint8_t prefixLen);
void initFromAddressAndNetmask(const IPv4Address& address, const IPv4Address& netmaskAddress);
};
Expand All @@ -521,7 +523,7 @@ namespace pcpp
/// A constructor that creates an instance of the class out of an address and a full prefix length,
/// essentially making a network of consisting of only 1 address.
/// @param address An address representing the network prefix.
explicit IPv6Network(const IPv6Address& address) : IPv6Network(address, 128u)
explicit IPv6Network(const IPv6Address& address) : IPv6Network(address, 128U)
{}

/// A constructor that creates an instance of the class out of an address representing the network prefix
Expand Down Expand Up @@ -564,7 +566,7 @@ namespace pcpp
/// @return The network prefix, for example: the network prefix of 3546:f321::/16 is 3546::
IPv6Address getNetworkPrefix() const
{
return IPv6Address(m_NetworkPrefix);
return { m_NetworkPrefix };
}

/// @return The lowest non-reserved IPv6 address in this network, for example: the lowest address in 3546::/16
Expand Down Expand Up @@ -593,10 +595,10 @@ namespace pcpp
std::string toString() const;

private:
uint8_t m_NetworkPrefix[16];
uint8_t m_Mask[16];
uint8_t m_NetworkPrefix[16]{};
uint8_t m_Mask[16]{};
seladb marked this conversation as resolved.
Show resolved Hide resolved

bool isValidNetmask(const IPv6Address& netmaskAddress);
static bool isValidNetmask(const IPv6Address& netmaskAddress);
void initFromAddressAndPrefixLength(const IPv6Address& address, uint8_t prefixLen);
void initFromAddressAndNetmask(const IPv6Address& address, const IPv6Address& netmaskAddress);
};
Expand All @@ -609,7 +611,7 @@ namespace pcpp
/// A constructor that creates an instance of the class out of an IP address and a full prefix length,
/// essentially making a network of consisting of only 1 address.
/// @param address An address representing the network prefix.
explicit IPNetwork(const IPAddress& address) : IPNetwork(address, address.isIPv4() ? 32u : 128u)
explicit IPNetwork(const IPAddress& address) : IPNetwork(address, address.isIPv4() ? 32U : 128U)
{}

/// A constructor that creates an instance of the class out of an address representing the network prefix
Expand Down Expand Up @@ -692,26 +694,21 @@ namespace pcpp
/// @return A reference to the assignee
IPNetwork& operator=(const IPNetwork& other)
{
// NOLINTBEGIN(cppcoreguidelines-c-copy-assignment-signature,misc-unconventional-assign-operator)
if (other.isIPv4Network())
{
return this->operator=(*other.m_IPv4Network);
}
else
{
return this->operator=(*other.m_IPv6Network);
}

return this->operator=(*other.m_IPv6Network);
// NOLINTEND(cppcoreguidelines-c-copy-assignment-signature,misc-unconventional-assign-operator)
}

/// Overload of an assignment operator.
/// @param[in] other An instance of IPv4Network to assign
/// @return A reference to the assignee
IPNetwork& operator=(const IPv4Network& other)
{
if (m_IPv4Network)
{
m_IPv4Network = nullptr;
}

if (m_IPv6Network)
{
m_IPv6Network = nullptr;
Expand All @@ -732,11 +729,6 @@ namespace pcpp
m_IPv4Network = nullptr;
}

if (m_IPv6Network)
{
m_IPv6Network = nullptr;
}

m_IPv6Network = std::unique_ptr<IPv6Network>(new IPv6Network(other));

return *this;
Expand Down Expand Up @@ -814,15 +806,13 @@ namespace pcpp

return m_IPv4Network->includes(address.getIPv4());
}
else
{
if (address.isIPv4())
{
return false;
}

return m_IPv6Network->includes(address.getIPv6());
if (address.isIPv4())
{
return false;
}

return m_IPv6Network->includes(address.getIPv6());
}

/// @param network An IP network
Expand All @@ -838,15 +828,13 @@ namespace pcpp

return m_IPv4Network->includes(*network.m_IPv4Network);
}
else
{
if (network.isIPv4Network())
{
return false;
}

return m_IPv6Network->includes(*network.m_IPv6Network);
if (network.isIPv4Network())
{
return false;
}

return m_IPv6Network->includes(*network.m_IPv6Network);
}

/// @return A string representation of the network in a format of NETWORK_PREFIX/PREFIX_LEN, for example:
Expand All @@ -861,40 +849,40 @@ namespace pcpp
std::unique_ptr<IPv6Network> m_IPv6Network;
};

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv4Address& ipv4Address)
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPv4Address& ipv4Address)
{
os << ipv4Address.toString();
return os;
oss << ipv4Address.toString();
return oss;
}

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv6Address& ipv6Address)
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPv6Address& ipv6Address)
{
os << ipv6Address.toString();
return os;
oss << ipv6Address.toString();
return oss;
}

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPAddress& ipAddress)
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPAddress& ipAddress)
{
os << ipAddress.toString();
return os;
oss << ipAddress.toString();
return oss;
}

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv4Network& network)
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPv4Network& network)
{
os << network.toString();
return os;
oss << network.toString();
return oss;
}

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPv6Network& network)
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPv6Network& network)
{
os << network.toString();
return os;
oss << network.toString();
return oss;
}

inline std::ostream& operator<<(std::ostream& os, const pcpp::IPNetwork& network)
inline std::ostream& operator<<(std::ostream& oss, const pcpp::IPNetwork& network)
{
os << network.toString();
return os;
oss << network.toString();
return oss;
}

} // namespace pcpp
22 changes: 11 additions & 11 deletions Common++/header/IpUtils.h
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#pragma once

#include <stdint.h>
#include <cstdint>
#ifdef __linux__
# include <netinet/in.h>
# include <arpa/inet.h>
Expand Down Expand Up @@ -53,34 +53,34 @@ namespace pcpp
namespace internal
{
/// Extract IPv4 address from sockaddr
/// @param[in] sa - input sockaddr
/// @param[in] sAddr - input sockaddr
/// @return Address in in_addr format
/// @throws std::invalid_argument Sockaddr family is not AF_INET or sockaddr is nullptr.
in_addr* sockaddr2in_addr(sockaddr* sa);
in_addr* sockaddr2in_addr(sockaddr* sAddr);

/// Attempt to extract IPv4 address from sockaddr
/// @param[in] sa - input sockaddr
/// @param[in] sAddr - input sockaddr
/// @return Pointer to address in in_addr format or nullptr if extraction fails.
in_addr* try_sockaddr2in_addr(sockaddr* sa);
in_addr* try_sockaddr2in_addr(sockaddr* sAddr);

/// Extract IPv6 address from sockaddr
/// @param[in] sa - input sockaddr
/// @param[in] sAddr - input sockaddr
/// @return Address in in6_addr format
/// @throws std::invalid_argument Sockaddr family is not AF_INET6 or sockaddr is nullptr.
in6_addr* sockaddr2in6_addr(sockaddr* sa);
in6_addr* sockaddr2in6_addr(sockaddr* sAddr);

/// Attempt to extract IPv6 address from sockaddr
/// @param[in] sa - input sockaddr
/// @param[in] sAddr - input sockaddr
/// @return Pointer to address in in6_addr format or nullptr if extraction fails.
in6_addr* try_sockaddr2in6_addr(sockaddr* sa);
in6_addr* try_sockaddr2in6_addr(sockaddr* sAddr);

/// Converts a sockaddr format address to its string representation
/// @param[in] sa Address in sockaddr format
/// @param[in] sAddr Address in sockaddr format
/// @param[out] resultString String representation of the address
/// @param[in] resultBufLen Length of the result buffer.
/// @throws std::invalid_argument Sockaddr family is not AF_INET or AF_INET6, sockaddr is nullptr or the result
/// str buffer is insufficient.
void sockaddr2string(sockaddr const* sa, char* resultString, size_t resultBufLen);
void sockaddr2string(const sockaddr* sAddr, char* resultString, size_t resultBufLen);

/// Convert a in_addr format address to 32bit representation
/// @param[in] inAddr Address in in_addr format
Expand Down
16 changes: 9 additions & 7 deletions Common++/header/LRUList.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,15 +23,13 @@ namespace pcpp
template <typename T> class LRUList
{
public:
typedef typename std::list<T>::iterator ListIterator;
typedef typename std::unordered_map<T, ListIterator>::iterator MapIterator;
using ListIterator = typename std::list<T>::iterator;
using MapIterator = typename std::unordered_map<T, ListIterator>::iterator;

/// A c'tor for this class
/// @param[in] maxSize The max size this list can go
explicit LRUList(std::size_t maxSize)
{
m_MaxSize = maxSize;
}
explicit LRUList(std::size_t maxSize) : m_MaxSize(maxSize)
{}

/// Puts an element in the list. This element will be inserted (or advanced if it already exists) to the head of
/// the list as the most recently used element. If the list already reached its max size and the element is new
Expand All @@ -51,7 +49,7 @@ namespace pcpp
// iterator to the element that prevented the insertion
std::pair<MapIterator, bool> pair =
m_CacheItemsMap.insert(std::make_pair(element, m_CacheItemsList.begin()));
if (pair.second == false) // already exists
if (!pair.second) // already exists
{
m_CacheItemsList.erase(pair.first->second);
pair.first->second = m_CacheItemsList.begin();
Expand All @@ -63,11 +61,13 @@ namespace pcpp
--lruIter;

if (deletedValue != nullptr)
{
#if __cplusplus > 199711L || _MSC_VER >= 1800
*deletedValue = std::move(*lruIter);
#else
*deletedValue = *lruIter;
#endif
}
m_CacheItemsMap.erase(*lruIter);
m_CacheItemsList.erase(lruIter);
return 1;
Expand Down Expand Up @@ -96,7 +96,9 @@ namespace pcpp
{
MapIterator iter = m_CacheItemsMap.find(element);
if (iter == m_CacheItemsMap.end())
{
return;
}

m_CacheItemsList.erase(iter->second);
m_CacheItemsMap.erase(iter);
Expand Down
Loading
Loading