Skip to content

Commit

Permalink
Fixes various MSVC compiler warnings. (#1547)
Browse files Browse the repository at this point in the history
* Fixed MSVC C4129

* Fixed MSVC C6262.

* Attempt to suppress MSVC C26444 in PTF_ASSERT_RAISES.

* Fixed MSVC C26817

* Fixed MSVC C6387

* Fixed MSVC C26478

* Fixed C28182

* Fixed C6297

* Fixes C6011

* Fixed C26439.
  • Loading branch information
Dimi1010 authored Aug 25, 2024
1 parent fdd4caf commit 0a8c11b
Show file tree
Hide file tree
Showing 13 changed files with 50 additions and 21 deletions.
7 changes: 6 additions & 1 deletion Examples/DnsSpoofing/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* address as the resolved IP. Then it's sent back on the network on the same interface
*/

#include <stdexcept>
#include <vector>
#include <iostream>
#include <algorithm>
Expand Down Expand Up @@ -205,11 +206,15 @@ void handleDnsRequest(pcpp::RawPacket* packet, pcpp::PcapLiveDevice* dev, void*
ip4Layer->setDstIPv4Address(srcIP.getIPv4());
ip4Layer->getIPv4Header()->ipId = 0;
}
else
else if (ip6Layer != nullptr)
{
ip6Layer->setSrcIPv6Address(ip6Layer->getDstIPv6Address());
ip6Layer->setDstIPv6Address(srcIP.getIPv6());
}
else
{
throw std::logic_error("IPLayer should be either IPv4Layer or IPv6Layer");
}

// reverse src and dst UDP ports
uint16_t srcPort = udpLayer->getUdpHeader()->portSrc;
Expand Down
5 changes: 5 additions & 0 deletions Examples/IcmpFileTransfer/IcmpFileTransfer-pitcher.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
*/

#include <stdlib.h>
#include <stdexcept>
#include <iostream>
#include <fstream>
#ifndef _MSC_VER
Expand Down Expand Up @@ -37,6 +38,10 @@ void usleep(__int64 usec)
ft.QuadPart = -(10 * usec); // Convert to 100 nanosecond interval, negative value indicates relative time

timer = CreateWaitableTimer(NULL, TRUE, NULL);
if (timer == nullptr)
{
throw std::runtime_error("Could not create waitable timer with error: " + std::to_string(GetLastError()));
}
SetWaitableTimer(timer, &ft, 0, NULL, NULL, 0);
WaitForSingleObject(timer, INFINITE);
CloseHandle(timer);
Expand Down
8 changes: 4 additions & 4 deletions Packet++/header/HttpLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -710,13 +710,13 @@ namespace pcpp
class HttpRequestFirstLineException : public std::exception
{
public:
~HttpRequestFirstLineException() throw()
~HttpRequestFirstLineException() noexcept
{}
void setMessage(const std::string& message)
{
m_Message = message;
}
virtual const char* what() const throw()
virtual const char* what() const noexcept
{
return m_Message.c_str();
}
Expand Down Expand Up @@ -857,13 +857,13 @@ namespace pcpp
class HttpResponseFirstLineException : public std::exception
{
public:
~HttpResponseFirstLineException() throw()
~HttpResponseFirstLineException() noexcept
{}
void setMessage(const std::string& message)
{
m_Message = message;
}
virtual const char* what() const throw()
virtual const char* what() const noexcept
{
return m_Message.c_str();
}
Expand Down
8 changes: 4 additions & 4 deletions Packet++/header/SipLayer.h
Original file line number Diff line number Diff line change
Expand Up @@ -614,13 +614,13 @@ namespace pcpp
class SipRequestFirstLineException : public std::exception
{
public:
~SipRequestFirstLineException() throw()
~SipRequestFirstLineException() noexcept
{}
void setMessage(const std::string& message)
{
m_Message = message;
}
virtual const char* what() const throw()
virtual const char* what() const noexcept
{
return m_Message.c_str();
}
Expand Down Expand Up @@ -748,13 +748,13 @@ namespace pcpp
class SipResponseFirstLineException : public std::exception
{
public:
~SipResponseFirstLineException() throw()
~SipResponseFirstLineException() noexcept
{}
void setMessage(const std::string& message)
{
m_Message = message;
}
virtual const char* what() const throw()
virtual const char* what() const noexcept
{
return m_Message.c_str();
}
Expand Down
4 changes: 3 additions & 1 deletion Packet++/src/FtpLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,9 @@ namespace pcpp
std::string field = getCommandString();

for (size_t idx = 0; idx < field.size(); ++idx)
val |= (field.c_str()[idx] << (idx * 8));
{
val |= static_cast<size_t>(field.c_str()[idx]) << (idx * 8);
}

return static_cast<FtpCommand>(val);
}
Expand Down
5 changes: 5 additions & 0 deletions Packet++/src/IPv6Layer.cpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#define LOG_MODULE PacketLogModuleIPv6Layer

#include <stdexcept>
#include "IPv6Layer.h"
#include "IPv4Layer.h"
#include "PayloadLayer.h"
Expand Down Expand Up @@ -133,6 +134,10 @@ namespace pcpp
}
else
{
if (curExt == nullptr)
{
throw std::logic_error("curExt is nullptr");
}
curExt->setNextHeader(newExt);
curExt = curExt->getNextHeader();
}
Expand Down
4 changes: 2 additions & 2 deletions Packet++/src/SipLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -360,7 +360,7 @@ namespace pcpp
SipRequestLayer::SipRequestLayer(SipMethod method, const std::string& requestUri, const std::string& version)
{
m_Protocol = SIPRequest;
m_FirstLine = new SipRequestFirstLine(this, method, std::move(version), std::move(requestUri));
m_FirstLine = new SipRequestFirstLine(this, method, version, requestUri);
m_FieldsOffset = m_FirstLine->getSize();
}

Expand Down Expand Up @@ -598,7 +598,7 @@ namespace pcpp
const std::string& sipVersion)
{
m_Protocol = SIPResponse;
m_FirstLine = new SipResponseFirstLine(this, std::move(sipVersion), statusCode, std::move(statusCodeString));
m_FirstLine = new SipResponseFirstLine(this, sipVersion, statusCode, std::move(statusCodeString));
m_FieldsOffset = m_FirstLine->getSize();
}

Expand Down
4 changes: 3 additions & 1 deletion Packet++/src/SmtpLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,9 @@ namespace pcpp
std::string field = getCommandString();

for (size_t idx = 0; idx < field.size(); ++idx)
val |= (field.c_str()[idx] << (idx * 8));
{
val |= static_cast<size_t>(field.c_str()[idx]) << (idx * 8);
}

return static_cast<SmtpCommand>(val);
}
Expand Down
2 changes: 1 addition & 1 deletion Packet++/src/TextBasedProtocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -529,7 +529,7 @@ namespace pcpp
{
m_NameValueSeparator = nameValueSeparator;
m_SpacesAllowedBetweenNameAndValue = spacesAllowedBetweenNameAndValue;
initNewField(std::move(name), std::move(value));
initNewField(name, value);
}

void HeaderField::initNewField(const std::string& name, const std::string& value)
Expand Down
2 changes: 1 addition & 1 deletion Packet++/src/VrrpLayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ namespace pcpp

size_t ipAddrOffset = 0;
uint8_t* newIpAddresses = getData() + offset;
for (auto ipAddress : ipAddresses)
for (auto const& ipAddress : ipAddresses)
{
copyIPAddressToData(newIpAddresses + ipAddrOffset, ipAddress);
ipAddrOffset += ipAddrLen;
Expand Down
2 changes: 1 addition & 1 deletion Tests/Pcap++Test/Tests/IpMacTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -364,7 +364,7 @@ PTF_TEST_CASE(TestGetMacAddress)
std::string ipsInArpTableAsString;
#ifdef _WIN32
ipsInArpTableAsString =
pcpp::executeShellCommand("arp -a | for /f \"tokens=1\" \%i in ('findstr dynamic') do @echo \%i");
pcpp::executeShellCommand(R"(arp -a | for /f "tokens=1" %i in ('findstr dynamic') do @echo %i)");
ipsInArpTableAsString.erase(std::remove(ipsInArpTableAsString.begin(), ipsInArpTableAsString.end(), ' '),
ipsInArpTableAsString.end());
#else
Expand Down
13 changes: 8 additions & 5 deletions Tests/Pcap++Test/Tests/LiveDeviceTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@
#include "../Common/TestUtils.h"
#include "../Common/PcapFileNamesDef.h"
#include <array>
#include <vector>
#include <iterator>
#include <algorithm>
#include <cstdio>
#if defined(_WIN32)
Expand Down Expand Up @@ -821,9 +823,8 @@ PTF_TEST_CASE(TestSendPackets)
pcpp::PcapFileReaderDevice fileReaderDev(EXAMPLE_PCAP_PATH);
PTF_ASSERT_TRUE(fileReaderDev.open());

pcpp::RawPacket rawPacketArr[10000];
std::vector<pcpp::RawPacket> rawPacketArr(10000);
pcpp::PointerVector<pcpp::Packet> packetVec;
pcpp::Packet* packetArr[10000];
int packetsRead = 0;
while (fileReaderDev.getNextPacket(rawPacketArr[packetsRead]))
{
Expand All @@ -832,11 +833,13 @@ PTF_TEST_CASE(TestSendPackets)
}

// send packets as RawPacket array
int packetsSentAsRaw = liveDev->sendPackets(rawPacketArr, packetsRead);
int packetsSentAsRaw = liveDev->sendPackets(rawPacketArr.data(), packetsRead);

// send packets as parsed EthPacekt array
std::copy(packetVec.begin(), packetVec.end(), packetArr);
int packetsSentAsParsed = liveDev->sendPackets(packetArr, packetsRead);
std::vector<pcpp::Packet*> packetArr;
packetArr.reserve(10000);
std::copy(packetVec.begin(), packetVec.end(), std::back_inserter(packetArr));
int packetsSentAsParsed = liveDev->sendPackets(packetArr.data(), packetsRead);

PTF_ASSERT_EQUAL(packetsSentAsRaw, packetsRead);
PTF_ASSERT_EQUAL(packetsSentAsParsed, packetsRead);
Expand Down
7 changes: 7 additions & 0 deletions Tests/PcppTestFramework/PcppTestFramework.h
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,12 @@
#include "memplumber.h"
#include "PcppTestFrameworkCommon.h"

#ifdef _MSC_VER
# define _PTF_MSVC_SUPPRESS_C26444 _Pragma("warning(suppress: 26444)");
#else
# define _PTF_MSVC_SUPPRESS_C26444
#endif // _MSC_VER

#define _PTF_PRINT_TYPE_ACTUAL(exp, val) val
#define _PTF_PRINT_TYPE_EXPECTED(exp, val) val
#define hex_PTF_PRINT_TYPE_ACTUAL(exp, val) "0x" << std::hex << +val << std::dec
Expand Down Expand Up @@ -217,6 +223,7 @@
std::string messageCaught = ""; \
try \
{ \
_PTF_MSVC_SUPPRESS_C26444 \
expression; \
} \
catch (const exception_type& e) \
Expand Down

0 comments on commit 0a8c11b

Please sign in to comment.