Skip to content

Commit

Permalink
Fixed many clang-tidy warnings (#7)
Browse files Browse the repository at this point in the history
  • Loading branch information
FlorianReimold authored Dec 14, 2023
1 parent a462630 commit 9ec1681
Show file tree
Hide file tree
Showing 12 changed files with 234 additions and 160 deletions.
63 changes: 63 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
---
# Resons why specific warnings have been turned off:
#
# -clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling
# This warns about memcpy and wants us to use memcpy_s, which is not available in our gcc setup.
#
# -cppcoreguidelines-pro-type-vararg
# This forbids using functions like printf, snprintf etc. We would like to use those either way.
#
# -misc-no-recursion
# Recursion with functions can be an elegant way of solving recursive problems
#
# These checks have been disabled to keep compatibility with C++14:
# -modernize-concat-nested-namespaces
# -modernize-use-nodiscard
#

Checks: "-*,
clang-analyzer-*,
-clang-analyzer-security.insecureAPI.DeprecatedOrUnsafeBufferHandling,
bugprone-*,
-bugprone-easily-swappable-parameters,
-bugprone-implicit-widening-of-multiplication-result,
-bugprone-narrowing-conversions,
cppcoreguidelines-*,
-cppcoreguidelines-avoid-magic-numbers,
-cppcoreguidelines-avoid-non-const-global-variables,
-cppcoreguidelines-macro-usage,
-cppcoreguidelines-narrowing-conversions,
-cppcoreguidelines-non-private-member-variables-in-classes,
-cppcoreguidelines-pro-bounds-array-to-pointer-decay,
-cppcoreguidelines-pro-bounds-pointer-arithmetic,
-cppcoreguidelines-pro-type-vararg,
-cppcoreguidelines-pro-type-reinterpret-cast,
misc-*,
-misc-non-private-member-variables-in-classes,
-misc-no-recursion,
modernize-*,
-modernize-pass-by-value,
-modernize-use-trailing-return-type,
-modernize-use-auto,
-modernize-concat-nested-namespaces,
-modernize-return-braced-init-list,
-modernize-use-nodiscard,
-modernize-avoid-bind,
performance-*,
readability-*,
-readability-braces-around-statements,
-readability-identifier-length,
-readability-magic-numbers,
-readability-redundant-access-specifiers,
-readability-function-cognitive-complexity,
-readability-else-after-return,
"
WarningsAsErrors: ''
HeaderFilterRegex: '^((?!/thirdparty/|/_deps/).)*$'
FormatStyle: none
10 changes: 5 additions & 5 deletions samples/asio_sender_multicast/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,12 @@ int main()

asio::io_service io_service;

asio::ip::udp::endpoint endpoint(asio::ip::make_address("239.0.0.1"), 14000);
asio::ip::udp::socket upd_socket(io_service, endpoint.protocol());
const asio::ip::udp::endpoint endpoint(asio::ip::make_address("239.0.0.1"), 14000);
asio::ip::udp::socket upd_socket(io_service, endpoint.protocol());

// set multicast packet TTL
{
asio::ip::multicast::hops ttl(2);
const asio::ip::multicast::hops ttl(2);
asio::error_code ec;
upd_socket.set_option(ttl, ec);
if (ec)
Expand All @@ -49,9 +49,9 @@ int main()

// set loopback option
{
asio::ip::multicast::enable_loopback loopback(true);
const asio::ip::multicast::enable_loopback loopback(true);
asio::error_code ec;
upd_socket.set_option(loopback);
upd_socket.set_option(loopback, ec);
if (ec)
{
std::cerr << "ERROR: Error setting loopback option: " << ec.message() << std::endl;
Expand Down
4 changes: 2 additions & 2 deletions samples/asio_sender_unicast/src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ int main()

asio::io_service io_service;

asio::ip::udp::endpoint endpoint(asio::ip::make_address("127.0.0.1"), 14000);
asio::ip::udp::socket upd_socket(io_service, endpoint.protocol());
const asio::ip::udp::endpoint endpoint(asio::ip::make_address("127.0.0.1"), 14000);
asio::ip::udp::socket upd_socket(io_service, endpoint.protocol());

int counter = 0;
for(;;)
Expand Down
2 changes: 0 additions & 2 deletions udpcap/include/udpcap/host_address.h
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ namespace Udpcap
/** @brief Constructs a HostAddress from a 32bit integer in host byte order. */
UDPCAP_EXPORT HostAddress(const uint32_t address);

UDPCAP_EXPORT ~HostAddress();

/** @brief Checks if the Host Address is valid.
* Invalid HostAddresses are created when providing a wrong IPv4 string,
* using the empty default constructor or the HostAddress::Invalid()
Expand Down
9 changes: 7 additions & 2 deletions udpcap/include/udpcap/udpcap_socket.h
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,13 @@ namespace Udpcap
UDPCAP_EXPORT UdpcapSocket();
UDPCAP_EXPORT ~UdpcapSocket();

UDPCAP_EXPORT UdpcapSocket(UdpcapSocket const&) = delete;
UDPCAP_EXPORT UdpcapSocket& operator= (UdpcapSocket const&) = delete;
// Copy
UdpcapSocket(UdpcapSocket const&) = delete;
UdpcapSocket& operator= (UdpcapSocket const&) = delete;

// Move
UDPCAP_EXPORT UdpcapSocket& operator=(UdpcapSocket&&) noexcept;
UDPCAP_EXPORT UdpcapSocket(UdpcapSocket&&) noexcept;

/**
* @brief Checks whether the socket is valid (i.e. npcap has been intialized successfully)
Expand Down
13 changes: 6 additions & 7 deletions udpcap/src/host_address.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
#define NOMINMAX
#include <Ws2tcpip.h>

#include <array>

namespace Udpcap
{
////////////////////////////////
Expand All @@ -46,9 +48,6 @@ namespace Udpcap
, ipv4_(address)
{}

HostAddress::~HostAddress()
{}

bool HostAddress::isValid() const
{
return valid_;
Expand All @@ -70,7 +69,7 @@ namespace Udpcap
{
if (valid_)
{
uint32_t lower_byte = (ipv4_ & 0x000000FF);
const uint32_t lower_byte = (ipv4_ & 0x000000FF);
return (lower_byte >= 224) && (lower_byte <= 239);
}
else
Expand All @@ -83,9 +82,9 @@ namespace Udpcap
{
if (valid_)
{
char buffer[16];
inet_ntop(AF_INET, (void*)(&ipv4_), buffer, 16);
return std::string(buffer);
std::array<char, 16> buffer{};
inet_ntop(AF_INET, (void*)(&ipv4_), buffer.data(), 16);
return std::string(buffer.data());
}
else
{
Expand Down
13 changes: 7 additions & 6 deletions udpcap/src/ip_reassembly.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,22 +83,22 @@ namespace Udpcap
/// Helper functions
/////////////////////////////////////////

std::unique_ptr<pcpp::IPReassembly::PacketKey> IpReassembly::getPacketKey(pcpp::Packet* packet)
std::unique_ptr<pcpp::IPReassembly::PacketKey> IpReassembly::getPacketKey(const pcpp::Packet* packet)
{
{
pcpp::IPv4Layer* ipv4_layer = packet->getLayerOfType<pcpp::IPv4Layer>();
if (ipv4_layer)
if (ipv4_layer != nullptr)
{
return std::make_unique<pcpp::IPReassembly::IPv4PacketKey>(_byteswap_ushort(ipv4_layer->getIPv4Header()->ipId), ipv4_layer->getSrcIPv4Address(), ipv4_layer->getDstIPv4Address());
}
}

{
pcpp::IPv6Layer* ipv6_layer = packet->getLayerOfType<pcpp::IPv6Layer>();
if (ipv6_layer)
if (ipv6_layer != nullptr)
{
auto frag_header = ipv6_layer->getExtensionOfType<pcpp::IPv6FragmentationHeader>();
if (frag_header)
auto* frag_header = ipv6_layer->getExtensionOfType<pcpp::IPv6FragmentationHeader>();
if (frag_header != nullptr)
return std::make_unique<pcpp::IPReassembly::IPv6PacketKey>(ntohl(frag_header->getFragHeader()->id), ipv6_layer->getSrcIPv6Address(), ipv6_layer->getDstIPv6Address());
else
return nullptr;
Expand Down Expand Up @@ -149,7 +149,8 @@ namespace Udpcap
}
else
{
timestamp_map_.emplace(packet_key->getHashValue(), std::make_pair(std::move(packet_key), now));
auto hash_value = packet_key->getHashValue();
timestamp_map_.emplace(hash_value, std::make_pair(std::move(packet_key), now));
}
}

Expand Down
2 changes: 1 addition & 1 deletion udpcap/src/ip_reassembly.h
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ namespace Udpcap
/////////////////////////////////////////
private:

std::unique_ptr<pcpp::IPReassembly::PacketKey> getPacketKey(pcpp::Packet* packet);
static std::unique_ptr<pcpp::IPReassembly::PacketKey> getPacketKey(const pcpp::Packet* packet);

void removeOldPackages();
void removePackageFromTimestampMap(std::unique_ptr<pcpp::IPReassembly::PacketKey> packet_key);
Expand Down
Loading

0 comments on commit 9ec1681

Please sign in to comment.