From 3d8975d982c1fe38b104aeee3463d59c0c0ca177 Mon Sep 17 00:00:00 2001 From: "vincenzo.comito" Date: Wed, 29 Jan 2025 00:51:12 +0100 Subject: [PATCH] Fix free and formatting (#1656) --- Common++/src/SystemUtils.cpp | 8 ++++++-- Packet++/src/DnsResourceData.cpp | 3 ++- Packet++/src/VrrpLayer.cpp | 3 ++- Pcap++/header/PcapLiveDevice.h | 24 ++++++++-------------- Pcap++/src/PcapLiveDevice.cpp | 34 ++++++++++++++++++-------------- 5 files changed, 37 insertions(+), 35 deletions(-) diff --git a/Common++/src/SystemUtils.cpp b/Common++/src/SystemUtils.cpp index 91e76583f4..6dc85d8dba 100644 --- a/Common++/src/SystemUtils.cpp +++ b/Common++/src/SystemUtils.cpp @@ -200,7 +200,9 @@ namespace pcpp bool directoryExists(const std::string& dirPath) { - struct stat info{}; + struct stat info + { + }; if (stat(dirPath.c_str(), &info) != 0) { @@ -368,7 +370,9 @@ namespace pcpp #if defined(_WIN32) SetConsoleCtrlHandler((PHANDLER_ROUTINE)handlerRoutine, TRUE); #else - struct sigaction action{}; + struct sigaction action + { + }; memset(&action, 0, sizeof(struct sigaction)); action.sa_handler = handlerRoutine; sigemptyset(&action.sa_mask); diff --git a/Packet++/src/DnsResourceData.cpp b/Packet++/src/DnsResourceData.cpp index 5365954639..e735d447de 100644 --- a/Packet++/src/DnsResourceData.cpp +++ b/Packet++/src/DnsResourceData.cpp @@ -204,7 +204,8 @@ namespace pcpp { if (m_DataLen == 0 || m_Data == nullptr) { - PCPP_LOG_ERROR("Input data is null or illegal" << "|m_DataLen:" << m_DataLen); + PCPP_LOG_ERROR("Input data is null or illegal" + << "|m_DataLen:" << m_DataLen); return false; } diff --git a/Packet++/src/VrrpLayer.cpp b/Packet++/src/VrrpLayer.cpp index 8f391155a7..c56cd7f058 100644 --- a/Packet++/src/VrrpLayer.cpp +++ b/Packet++/src/VrrpLayer.cpp @@ -485,7 +485,8 @@ namespace pcpp auto* ipLayer = m_Packet->getLayerOfType(); if (ipLayer == nullptr) { - PCPP_LOG_ERROR("Calculate checksum failed, for can not get IPLayer" << ""); + PCPP_LOG_ERROR("Calculate checksum failed, for can not get IPLayer" + << ""); return 0; } diff --git a/Pcap++/header/PcapLiveDevice.h b/Pcap++/header/PcapLiveDevice.h index 082e353d51..ccb1f7f03d 100644 --- a/Pcap++/header/PcapLiveDevice.h +++ b/Pcap++/header/PcapLiveDevice.h @@ -169,10 +169,8 @@ namespace pcpp PCPP_OUT }; - /** - * Set which source provides timestamps associated to each captured packet - * (you can read more here: ) - */ + /// Set which source provides timestamps associated to each captured packet + /// (you can read more here: ) enum class TimestampProvider { /** host-provided, unknown characteristics, default */ @@ -189,10 +187,8 @@ namespace pcpp HostHighPrecUnsynced }; - /** - * Set the precision of timestamps associated to each captured packet - * (you can read more here: ) - */ + /// Set the precision of timestamps associated to each captured packet + /// (you can read more here: ) enum class TimestampPrecision { /** use timestamps with microsecond precision, default */ @@ -240,16 +236,12 @@ namespace pcpp /// In Unix-like system, use poll() for blocking mode. bool usePoll; - /** - * Set which timestamp provider is used. - * Depending on the capture device and the software on the host, different types of time stamp can be used - */ + /// Set which timestamp provider is used. + /// Depending on the capture device and the software on the host, different types of time stamp can be used TimestampProvider timestampProvider; - /** - * Set which timestamp precision is used. - * Depending on the capture device and the software on the host, different precision can be used - */ + /// Set which timestamp precision is used. + /// Depending on the capture device and the software on the host, different precision can be used TimestampPrecision timestampPrecision; /// A c'tor for this struct diff --git a/Pcap++/src/PcapLiveDevice.cpp b/Pcap++/src/PcapLiveDevice.cpp index 7a0712bcbc..1d3ec8460f 100644 --- a/Pcap++/src/PcapLiveDevice.cpp +++ b/Pcap++/src/PcapLiveDevice.cpp @@ -137,36 +137,40 @@ namespace pcpp static bool isTimestampProviderSupportedByDevice(pcap_t* pcap, const PcapLiveDevice::TimestampProvider timestampProvider) { - int tstampType = timestampProviderMap(timestampProvider); - int* supportedTstampTypes = nullptr; + const auto tstampType = timestampProviderMap(timestampProvider); + + // Use unique_ptr with a custom deleter directly + std::unique_ptr supportedTstampTypes(nullptr, [](int* ptr) { + if (ptr != nullptr) + { + pcap_free_tstamp_types(ptr); + } + }); + const int numSupportedTstampTypes = pcap_list_tstamp_types(pcap, &supportedTstampTypes); - bool isSupported = false; if (numSupportedTstampTypes < 0) { std::cerr << "Error retrieving timestamp types: " << pcap_geterr(pcap) << " - default Host will be used" << std::endl; - isSupported = false; + return false; } - else if (numSupportedTstampTypes == 1) + + if (numSupportedTstampTypes == 1) { - // If 1 is returned, then the only available typestamp is TimestampProvider::Host; + // If 1 is returned, then the only available timestamp is TimestampProvider::Host return timestampProvider == PcapLiveDevice::TimestampProvider::Host; } - else + + for (int i = 0; i < numSupportedTstampTypes; ++i) { - for (int i = 0; i < numSupportedTstampTypes; ++i) + if (supportedTstampTypes[i] == tstampType) { - if (supportedTstampTypes[i] == tstampType) - { - isSupported = true; - break; - } + return true; } } - pcap_free_tstamp_types(supportedTstampTypes); - return isSupported; + return false; } static void setTimestampProvider(pcap_t* pcap, const PcapLiveDevice::TimestampProvider timestampProvider)