Skip to content

Commit

Permalink
Fix cppcheck postfix operator issues (#1251)
Browse files Browse the repository at this point in the history
* fix some postfix operators

* more postfix

* const

* more postfix

* use stl

* use stl

* more fixes

* fixes

* use more const keyword

* more meaningful names

* suppress new stl warnings

* fix for const warn

* minor fixes

* minor fix

* minor fix

* not const

* update comment

---------

Co-authored-by: seladb <pcapplusplus@gmail.com>
  • Loading branch information
egecetin and seladb authored Dec 14, 2023
1 parent c46c99f commit 5fa0f7d
Show file tree
Hide file tree
Showing 38 changed files with 288 additions and 313 deletions.
2 changes: 1 addition & 1 deletion Common++/header/LRUList.h
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ namespace pcpp
if (m_CacheItemsMap.size() > m_MaxSize)
{
ListIterator lruIter = m_CacheItemsList.end();
lruIter--;
--lruIter;

if (deletedValue != NULL)
#if __cplusplus > 199711L || _MSC_VER >= 1800
Expand Down
12 changes: 6 additions & 6 deletions Common++/header/PointerVector.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,9 @@ namespace pcpp
*/
~PointerVector()
{
for (VectorIterator iter = m_Vector.begin(); iter != m_Vector.end(); iter++)
for (auto iter : m_Vector)
{
delete (*iter);
delete iter;
}
}

Expand All @@ -56,9 +56,9 @@ namespace pcpp
*/
PointerVector(const PointerVector& other)
{
for (ConstVectorIterator iter = other.begin(); iter != other.end(); iter++)
for (const auto iter : other)
{
T* objCopy = new T(**iter);
T* objCopy = new T(*iter);
m_Vector.push_back(objCopy);
}
}
Expand All @@ -68,9 +68,9 @@ namespace pcpp
*/
void clear()
{
for (VectorIterator iter = m_Vector.begin(); iter != m_Vector.end(); iter++)
for (auto iter : m_Vector)
{
delete (*iter);
delete iter;
}

m_Vector.clear();
Expand Down
4 changes: 2 additions & 2 deletions Common++/header/SystemUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -207,15 +207,15 @@ namespace pcpp
* @param[in] cores A vector of SystemCore instances
* @return A core mask representing these cores
*/
CoreMask createCoreMaskFromCoreVector(std::vector<SystemCore> cores);
CoreMask createCoreMaskFromCoreVector(const std::vector<SystemCore> &cores);


/**
* Create a core mask from a vector of core IDs
* @param[in] coreIds A vector of core IDs
* @return A core mask representing these cores
*/
CoreMask createCoreMaskFromCoreIds(std::vector<int> coreIds);
CoreMask createCoreMaskFromCoreIds(const std::vector<int> &coreIds);


/**
Expand Down
14 changes: 8 additions & 6 deletions Common++/src/SystemUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -144,23 +144,25 @@ CoreMask getCoreMaskForAllMachineCores()
return result;
}

CoreMask createCoreMaskFromCoreVector(std::vector<SystemCore> cores)
CoreMask createCoreMaskFromCoreVector(const std::vector<SystemCore> &cores)
{
CoreMask result = 0;
for (std::vector<SystemCore>::iterator iter = cores.begin(); iter != cores.end(); iter++)
for (const auto &core : cores)
{
result |= iter->Mask;
// cppcheck-suppress useStlAlgorithm
result |= core.Mask;
}

return result;
}

CoreMask createCoreMaskFromCoreIds(std::vector<int> coreIds)
CoreMask createCoreMaskFromCoreIds(const std::vector<int> &coreIds)
{
CoreMask result = 0;
for (std::vector<int>::iterator iter = coreIds.begin(); iter != coreIds.end(); iter++)
for (const auto &coreId : coreIds)
{
result |= SystemCores::IdToSystemCore[*iter].Mask;
// cppcheck-suppress useStlAlgorithm
result |= SystemCores::IdToSystemCore[coreId].Mask;
}

return result;
Expand Down
15 changes: 3 additions & 12 deletions Common++/src/TablePrinter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <sstream>
#include <iostream>
#include <iterator>
#include <numeric>
#include <utility>
#include "TablePrinter.h"
#include "Logger.h"
Expand Down Expand Up @@ -88,18 +89,8 @@ void TablePrinter::printSeparator()
return;
}

int totalLen = 0;
for (std::vector<int>::iterator iter = m_ColumnWidths.begin(); iter != m_ColumnWidths.end(); iter++)
{
totalLen += 2 + (*iter) + 1;
}

totalLen++;

for (int index = 0; index < totalLen; index++)
std::cout << "-";

std::cout << std::endl;
auto totalLen = std::accumulate(m_ColumnWidths.begin(), m_ColumnWidths.end(), m_ColumnWidths.size() * 3) + 1;
std::cout << std::string(totalLen, '-') << std::endl;
}

void TablePrinter::closeTable()
Expand Down
4 changes: 2 additions & 2 deletions Examples/Arping/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,9 @@ void listInterfaces()
const std::vector<pcpp::PcapLiveDevice*>& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList();

std::cout << std::endl << "Network interfaces:" << std::endl;
for (std::vector<pcpp::PcapLiveDevice*>::const_iterator iter = devList.begin(); iter != devList.end(); iter++)
for (const auto &dev : devList)
{
std::cout << " -> Name: '" << (*iter)->getName() << "' IP address: " << (*iter)->getIPv4Address().toString() << std::endl;
std::cout << " -> Name: '" << dev->getName() << "' IP address: " << dev->getIPv4Address().toString() << std::endl;
}
exit(0);
}
Expand Down
14 changes: 6 additions & 8 deletions Examples/DNSResolver/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,9 @@ void listInterfaces()
const std::vector<pcpp::PcapLiveDevice*>& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList();

std::cout << std::endl << "Network interfaces:" << std::endl;
for (std::vector<pcpp::PcapLiveDevice*>::const_iterator iter = devList.begin(); iter != devList.end(); iter++)
for (const auto &dev : devList)
{
std::cout << " -> Name: '" << (*iter)->getName() << "' IP address: " << (*iter)->getIPv4Address().toString() << std::endl;
std::cout << " -> Name: '" << dev->getName() << "' IP address: " << dev->getIPv4Address().toString() << std::endl;
}
exit(0);
}
Expand Down Expand Up @@ -189,13 +189,11 @@ int main(int argc, char* argv[])
{
const std::vector<pcpp::PcapLiveDevice*>& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList();

for (std::vector<pcpp::PcapLiveDevice*>::const_iterator iter = devList.begin(); iter != devList.end(); iter++)
auto iter = std::find_if(devList.begin(), devList.end(),
[](pcpp::PcapLiveDevice *dev) { return dev->getDefaultGateway().isValid(); });
if (iter != devList.end())
{
if ((*iter)->getDefaultGateway().isValid())
{
dev = *iter;
break;
}
dev = *iter;
}

if (dev == nullptr)
Expand Down
14 changes: 6 additions & 8 deletions Examples/DnsSpoofing/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,9 +128,9 @@ void listInterfaces()
const std::vector<pcpp::PcapLiveDevice*>& devList = pcpp::PcapLiveDeviceList::getInstance().getPcapLiveDevicesList();

std::cout << std::endl << "Network interfaces:" << std::endl;
for (std::vector<pcpp::PcapLiveDevice*>::const_iterator iter = devList.begin(); iter != devList.end(); iter++)
for (const auto &dev : devList)
{
std::cout << " -> Name: '" << (*iter)->getName() << "' IP address: " << (*iter)->getIPv4Address().toString() << std::endl;
std::cout << " -> Name: '" << dev->getName() << "' IP address: " << dev->getIPv4Address().toString() << std::endl;
}
exit(0);
}
Expand Down Expand Up @@ -172,9 +172,9 @@ void handleDnsRequest(pcpp::RawPacket* packet, pcpp::PcapLiveDevice* dev, void*
bool hostMatch = false;

// go over all hosts in dnsHostsToSpoof list and see if current query matches one of them
for (std::vector<std::string>::iterator iter = args->dnsHostsToSpoof.begin(); iter != args->dnsHostsToSpoof.end(); iter++)
for (const auto &host : args->dnsHostsToSpoof)
{
if (dnsLayer->getQuery(*iter, false) != nullptr)
if (dnsLayer->getQuery(host, false) != nullptr)
{
hostMatch = true;
break;
Expand Down Expand Up @@ -288,12 +288,10 @@ void onApplicationInterrupted(void* cookie)
std::sort(map2vec.begin(),map2vec.end(), &stringCountComparer);

// go over all items (hosts + count) in the sorted vector and print them
for(std::vector<std::pair<std::string, int> >::iterator iter = map2vec.begin();
iter != map2vec.end();
iter++)
for(const auto &iter : map2vec)
{
std::stringstream values;
values << iter->first << "|" << iter->second;
values << iter.first << "|" << iter.second;
printer.printRow(values.str(), '|');
}
}
Expand Down
16 changes: 8 additions & 8 deletions Examples/DpdkBridge/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -112,9 +112,9 @@ void listDpdkPorts()

// go over all available DPDK devices and print info for each one
std::vector<pcpp::DpdkDevice*> deviceList = pcpp::DpdkDeviceList::getInstance().getDpdkDeviceList();
for (std::vector<pcpp::DpdkDevice*>::iterator iter = deviceList.begin(); iter != deviceList.end(); iter++)
for (const auto &iter : deviceList)
{
pcpp::DpdkDevice* dev = *iter;
pcpp::DpdkDevice* dev = iter;
std::cout << " "
<< " Port #" << dev->getDeviceId() << ":"
<< " MAC address='" << dev->getMacAddress() << "';"
Expand Down Expand Up @@ -311,22 +311,22 @@ int main(int argc, char* argv[])

// collect the list of DPDK devices
std::vector<pcpp::DpdkDevice*> dpdkDevicesToUse;
for (std::vector<int>::iterator iter = dpdkPortVec.begin(); iter != dpdkPortVec.end(); iter++)
for (const auto &port : dpdkPortVec)
{
pcpp::DpdkDevice* dev = pcpp::DpdkDeviceList::getInstance().getDeviceByPort(*iter);
pcpp::DpdkDevice* dev = pcpp::DpdkDeviceList::getInstance().getDeviceByPort(port);
if (dev == NULL)
{
EXIT_WITH_ERROR("DPDK device for port " << *iter << " doesn't exist");
EXIT_WITH_ERROR("DPDK device for port " << port << " doesn't exist");
}
dpdkDevicesToUse.push_back(dev);
}

// go over all devices and open them
for (std::vector<pcpp::DpdkDevice*>::iterator iter = dpdkDevicesToUse.begin(); iter != dpdkDevicesToUse.end(); iter++)
for (const auto &dev : dpdkDevicesToUse)
{
if (!(*iter)->openMultiQueues(queueQuantity, 1))
if (!dev->openMultiQueues(queueQuantity, 1))
{
EXIT_WITH_ERROR("Couldn't open DPDK device #" << (*iter)->getDeviceId() << ", PMD '" << (*iter)->getPMDName() << "'");
EXIT_WITH_ERROR("Couldn't open DPDK device #" << dev->getDeviceId() << ", PMD '" << dev->getPMDName() << "'");
}
}

Expand Down
10 changes: 5 additions & 5 deletions Examples/DpdkExample-FilterTraffic/AppWorkerThread.h
Original file line number Diff line number Diff line change
Expand Up @@ -74,15 +74,15 @@ class AppWorkerThread : public pcpp::DpdkWorkerThread
while (!m_Stop)
{
// go over all DPDK devices configured for this worker/core
for (InputDataConfig::iterator iter = m_WorkerConfig.InDataCfg.begin(); iter != m_WorkerConfig.InDataCfg.end(); iter++)
for (const auto &iter : m_WorkerConfig.InDataCfg)
{
// for each DPDK device go over all RX queues configured for this worker/core
for (std::vector<int>::iterator iter2 = iter->second.begin(); iter2 != iter->second.end(); iter2++)
for (const auto &iter2 : iter.second)
{
pcpp::DpdkDevice* dev = iter->first;
pcpp::DpdkDevice* dev = iter.first;

// receive packets from network on the specified DPDK device and RX queue
uint16_t packetsReceived = dev->receivePackets(packetArr, MAX_RECEIVE_BURST, *iter2);
uint16_t packetsReceived = dev->receivePackets(packetArr, MAX_RECEIVE_BURST, iter2);

for (int i = 0; i < packetsReceived; i++)
{
Expand All @@ -96,7 +96,7 @@ class AppWorkerThread : public pcpp::DpdkWorkerThread

// hash the packet by 5-tuple and look in the flow table to see whether this packet belongs to an existing or new flow
uint32_t hash = pcpp::hash5Tuple(&parsedPacket);
std::map<uint32_t, bool>::const_iterator iter3 = m_FlowTable.find(hash);
auto iter3 = m_FlowTable.find(hash);

// if packet belongs to an already existing flow
if (iter3 != m_FlowTable.end() && iter3->second)
Expand Down
Loading

0 comments on commit 5fa0f7d

Please sign in to comment.