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

Fix cppcheck postfix operator issues #1251

Merged
merged 20 commits into from
Dec 14, 2023
Merged
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
10 changes: 6 additions & 4 deletions Common++/src/SystemUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -147,9 +147,10 @@ CoreMask getCoreMaskForAllMachineCores()
CoreMask createCoreMaskFromCoreVector(std::vector<SystemCore> cores)
{
CoreMask result = 0;
for (std::vector<SystemCore>::iterator iter = cores.begin(); iter != cores.end(); iter++)
for (const auto &iter : cores)
{
result |= iter->Mask;
// cppcheck-suppress useStlAlgorithm
result |= iter.Mask;
}

return result;
Expand All @@ -158,9 +159,10 @@ CoreMask createCoreMaskFromCoreVector(std::vector<SystemCore> cores)
CoreMask createCoreMaskFromCoreIds(std::vector<int> coreIds)
{
CoreMask result = 0;
for (std::vector<int>::iterator iter = coreIds.begin(); iter != coreIds.end(); iter++)
for (const auto &iter : coreIds)
{
result |= SystemCores::IdToSystemCore[*iter].Mask;
// cppcheck-suppress useStlAlgorithm
result |= SystemCores::IdToSystemCore[iter].Mask;
}

return result;
Expand Down
9 changes: 2 additions & 7 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,13 +89,7 @@ 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++;
int totalLen = std::accumulate(m_ColumnWidths.begin(), m_ColumnWidths.end(), m_ColumnWidths.size() * 3) + 1;
Copy link
Collaborator

Choose a reason for hiding this comment

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

more clearer!

Copy link
Owner

Choose a reason for hiding this comment

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

nit: maybe use auto for totalLen and for the for loop later?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

@seladb What about this e38dfa1 ? I also removed for loop and use string constructor


for (int index = 0; index < totalLen; index++)
std::cout << "-";
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 &iter : devList)
{
std::cout << " -> Name: '" << (*iter)->getName() << "' IP address: " << (*iter)->getIPv4Address().toString() << std::endl;
std::cout << " -> Name: '" << iter->getName() << "' IP address: " << iter->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 &iter : devList)
{
std::cout << " -> Name: '" << (*iter)->getName() << "' IP address: " << (*iter)->getIPv4Address().toString() << std::endl;
std::cout << " -> Name: '" << iter->getName() << "' IP address: " << iter->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 &iter : devList)
{
std::cout << " -> Name: '" << (*iter)->getName() << "' IP address: " << (*iter)->getIPv4Address().toString() << std::endl;
std::cout << " -> Name: '" << iter->getName() << "' IP address: " << iter->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 &iter : args->dnsHostsToSpoof)
{
if (dnsLayer->getQuery(*iter, false) != nullptr)
if (dnsLayer->getQuery(iter, 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 &iter : dpdkPortVec)
{
pcpp::DpdkDevice* dev = pcpp::DpdkDeviceList::getInstance().getDeviceByPort(*iter);
pcpp::DpdkDevice* dev = pcpp::DpdkDeviceList::getInstance().getDeviceByPort(iter);
if (dev == NULL)
{
EXIT_WITH_ERROR("DPDK device for port " << *iter << " doesn't exist");
EXIT_WITH_ERROR("DPDK device for port " << iter << " 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 &iter : dpdkDevicesToUse)
{
if (!(*iter)->openMultiQueues(queueQuantity, 1))
if (!iter->openMultiQueues(queueQuantity, 1))
{
EXIT_WITH_ERROR("Couldn't open DPDK device #" << (*iter)->getDeviceId() << ", PMD '" << (*iter)->getPMDName() << "'");
EXIT_WITH_ERROR("Couldn't open DPDK device #" << iter->getDeviceId() << ", PMD '" << iter->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
60 changes: 30 additions & 30 deletions Examples/DpdkExample-FilterTraffic/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -136,9 +136,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 All @@ -160,11 +160,11 @@ void prepareCoreConfiguration(std::vector<pcpp::DpdkDevice*>& dpdkDevicesToUse,
// create a list of pairs of DpdkDevice and RX queues for all RX queues in all requested devices
int totalNumOfRxQueues = 0;
std::vector<std::pair<pcpp::DpdkDevice*, int> > deviceAndRxQVec;
for (std::vector<pcpp::DpdkDevice*>::iterator iter = dpdkDevicesToUse.begin(); iter != dpdkDevicesToUse.end(); iter++)
for (const auto &iter : dpdkDevicesToUse)
{
for (int rxQueueIndex = 0; rxQueueIndex < rxQueues; rxQueueIndex++)
{
std::pair<pcpp::DpdkDevice*, int> curPair(*iter, rxQueueIndex);
std::pair<pcpp::DpdkDevice*, int> curPair(iter, rxQueueIndex);
deviceAndRxQVec.push_back(curPair);
}
totalNumOfRxQueues += rxQueues;
Expand All @@ -177,10 +177,10 @@ void prepareCoreConfiguration(std::vector<pcpp::DpdkDevice*>& dpdkDevicesToUse,
// prepare the configuration for every core: divide the devices and RX queue for each device with the various cores
int i = 0;
std::vector<std::pair<pcpp::DpdkDevice*, int> >::iterator pairVecIter = deviceAndRxQVec.begin();
for (std::vector<pcpp::SystemCore>::iterator iter = coresToUse.begin(); iter != coresToUse.end(); iter++)
for (const auto &iter : coresToUse)
{
std::cout << "Using core " << (int)iter->Id << std::endl;
workerConfigArr[i].CoreId = iter->Id;
std::cout << "Using core " << (int)iter.Id << std::endl;
workerConfigArr[i].CoreId = iter.Id;
workerConfigArr[i].WriteMatchedPacketsToFile = writePacketsToDisk;

std::stringstream packetFileName;
Expand All @@ -193,23 +193,23 @@ void prepareCoreConfiguration(std::vector<pcpp::DpdkDevice*>& dpdkDevicesToUse,
if (pairVecIter == deviceAndRxQVec.end())
break;
workerConfigArr[i].InDataCfg[pairVecIter->first].push_back(pairVecIter->second);
pairVecIter++;
++pairVecIter;
}
if (rxQueuesRemainder > 0 && (pairVecIter != deviceAndRxQVec.end()))
{
workerConfigArr[i].InDataCfg[pairVecIter->first].push_back(pairVecIter->second);
pairVecIter++;
++pairVecIter;
rxQueuesRemainder--;
}

// print configuration for core
std::cout << " Core configuration:" << std::endl;
for (InputDataConfig::iterator iter2 = workerConfigArr[i].InDataCfg.begin(); iter2 != workerConfigArr[i].InDataCfg.end(); iter2++)
for (const auto &iter2 : workerConfigArr[i].InDataCfg)
{
std::cout << " DPDK device#" << iter2->first->getDeviceId() << ": ";
for (std::vector<int>::iterator iter3 = iter2->second.begin(); iter3 != iter2->second.end(); iter3++)
std::cout << " DPDK device#" << iter2.first->getDeviceId() << ": ";
for (const auto &iter3 : iter2.second)
{
std::cout << "RX-Queue#" << *iter3 << "; ";
std::cout << "RX-Queue#" << iter3 << "; ";
}
std::cout << std::endl;
}
Expand Down Expand Up @@ -250,9 +250,9 @@ void onApplicationInterrupted(void* cookie)

// print final stats for every worker thread plus sum of all threads and free worker threads memory
PacketStats aggregatedStats;
for (std::vector<pcpp::DpdkWorkerThread*>::iterator iter = args->workerThreadsVector->begin(); iter != args->workerThreadsVector->end(); iter++)
for (const auto &iter : *(args->workerThreadsVector))
{
AppWorkerThread* thread = (AppWorkerThread*)(*iter);
AppWorkerThread* thread = (AppWorkerThread*)(iter);
PacketStats threadStats = thread->getStats();
aggregatedStats.collectStats(threadStats);
printer.printRow(threadStats.getStatValuesAsString("|"), '|');
Expand Down Expand Up @@ -489,39 +489,39 @@ 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 &iter : dpdkPortVec)
{
pcpp::DpdkDevice* dev = pcpp::DpdkDeviceList::getInstance().getDeviceByPort(*iter);
pcpp::DpdkDevice* dev = pcpp::DpdkDeviceList::getInstance().getDeviceByPort(iter);
if (dev == NULL)
{
EXIT_WITH_ERROR("DPDK device for port " << *iter << " doesn't exist");
EXIT_WITH_ERROR("DPDK device for port " << iter << " 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 &iter : dpdkDevicesToUse)
{
if (rxQueues > (*iter)->getTotalNumOfRxQueues())
if (rxQueues > iter->getTotalNumOfRxQueues())
{
EXIT_WITH_ERROR("Number of RX errors cannot exceed the max allowed by the device which is " << (*iter)->getTotalNumOfRxQueues());
EXIT_WITH_ERROR("Number of RX errors cannot exceed the max allowed by the device which is " << iter->getTotalNumOfRxQueues());
}
if (txQueues > (*iter)->getTotalNumOfTxQueues())
if (txQueues > iter->getTotalNumOfTxQueues())
{
EXIT_WITH_ERROR("Number of TX errors cannot exceed the max allowed by the device which is " << (*iter)->getTotalNumOfTxQueues());
EXIT_WITH_ERROR("Number of TX errors cannot exceed the max allowed by the device which is " << iter->getTotalNumOfTxQueues());
}
if (!(*iter)->openMultiQueues(rxQueues, txQueues))
if (!iter->openMultiQueues(rxQueues, txQueues))
{
EXIT_WITH_ERROR("Couldn't open DPDK device #" << (*iter)->getDeviceId() << ", PMD '" << (*iter)->getPMDName() << "'");
EXIT_WITH_ERROR("Couldn't open DPDK device #" << iter->getDeviceId() << ", PMD '" << iter->getPMDName() << "'");
}
std::cout
<< "Opened device #" << (*iter)->getDeviceId()
<< "Opened device #" << iter->getDeviceId()
<< " with " << rxQueues << " RX queues and " << txQueues << " TX queues."
<< " RSS hash functions:" << std::endl;
std::vector<std::string> rssHashFunctions = (*iter)->rssHashFunctionMaskToString((*iter)->getConfiguredRssHashFunction());
for(std::vector<std::string>::iterator it = rssHashFunctions.begin(); it != rssHashFunctions.end(); ++it)
std::vector<std::string> rssHashFunctions = iter->rssHashFunctionMaskToString(iter->getConfiguredRssHashFunction());
for(const auto &it : rssHashFunctions)
{
std::cout << " " << (*it) << std::endl;
std::cout << " " << it << std::endl;
}
}

Expand All @@ -541,7 +541,7 @@ int main(int argc, char* argv[])
// create worker thread for every core
std::vector<pcpp::DpdkWorkerThread*> workerThreadVec;
int i = 0;
for (std::vector<pcpp::SystemCore>::iterator iter = coresToUse.begin(); iter != coresToUse.end(); iter++)
for (auto iter = coresToUse.begin(); iter != coresToUse.end(); ++iter)
{
AppWorkerThread* newWorker = new AppWorkerThread(workerConfigArr[i], matchingEngine);
workerThreadVec.push_back(newWorker);
Expand Down
Loading
Loading