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

Replaced some C-style casts with Cpp style casts. #1383

Merged
merged 11 commits into from
May 6, 2024
4 changes: 2 additions & 2 deletions Common++/header/IpAddress.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,10 +92,10 @@ namespace pcpp
bool operator<(const IPv4Address& rhs) const
{
uint32_t intVal = toInt();
std::reverse((uint8_t*)(&intVal), (uint8_t*)(&intVal) + sizeof(intVal));
std::reverse(reinterpret_cast<uint8_t*>(&intVal), reinterpret_cast<uint8_t*>(&intVal) + sizeof(intVal));

uint32_t rhsIntVal = rhs.toInt();
std::reverse((uint8_t*)(&rhsIntVal), (uint8_t*)(&rhsIntVal) + sizeof(rhsIntVal));
std::reverse(reinterpret_cast<uint8_t*>(&rhsIntVal), reinterpret_cast<uint8_t*>(&rhsIntVal) + sizeof(rhsIntVal));

return intVal < rhsIntVal;
}
Expand Down
2 changes: 1 addition & 1 deletion Packet++/header/TLVData.h
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@ namespace pcpp
*/
void assign(uint8_t* recordRawData)
{
m_Data = (TLVRawData*)recordRawData;
m_Data = reinterpret_cast<TLVRawData*>(recordRawData);
}

/**
Expand Down
6 changes: 3 additions & 3 deletions Tests/Packet++Test/Tests/GtpTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ PTF_TEST_CASE(GtpLayerCreationTest)
PTF_ASSERT_EQUAL(newExt1.getExtensionType(), 0xc0);
PTF_ASSERT_EQUAL(newExt1.getTotalLength(), 4*sizeof(uint8_t));
PTF_ASSERT_EQUAL(newExt1.getContentLength(), 2*sizeof(uint8_t));
uint16_t* content = (uint16_t*)newExt1.getContent();
uint16_t* content = reinterpret_cast<uint16_t*>(newExt1.getContent());
PTF_ASSERT_EQUAL(be16toh(content[0]), 2308);
PTF_ASSERT_TRUE(newExt1.getNextExtension().isNull());

Expand All @@ -222,7 +222,7 @@ PTF_TEST_CASE(GtpLayerCreationTest)
PTF_ASSERT_EQUAL(newExt2.getExtensionType(), 0x40);
PTF_ASSERT_EQUAL(newExt2.getTotalLength(), 4*sizeof(uint8_t));
PTF_ASSERT_EQUAL(newExt2.getContentLength(), 2*sizeof(uint8_t));
content = (uint16_t*)newExt2.getContent();
content = reinterpret_cast<uint16_t*>(newExt2.getContent());
PTF_ASSERT_EQUAL(be16toh(content[0]), 1308);
PTF_ASSERT_TRUE(newExt2.getNextExtension().isNull());

Expand Down Expand Up @@ -276,7 +276,7 @@ PTF_TEST_CASE(GtpLayerEditTest)

pcpp::GtpV1Layer::GtpExtension gtpExtension = gtpLayer->getNextExtension();
PTF_ASSERT_FALSE(gtpExtension.isNull());
uint16_t* extContent = (uint16_t*)gtpExtension.getContent();
uint16_t* extContent = reinterpret_cast<uint16_t*>(gtpExtension.getContent());
PTF_ASSERT_EQUAL(be16toh(extContent[0]), 1000);

gtpHeader = gtpLayer->getHeader();
Expand Down
2 changes: 1 addition & 1 deletion Tests/Packet++Test/Tests/TcpTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -296,7 +296,7 @@ PTF_TEST_CASE(TcpChecksumInvalidRead)
m[2] = 0xF3;

pcpp::ScalarBuffer<uint16_t> vec[1];
vec[0].buffer = (uint16_t*)m;
vec[0].buffer = reinterpret_cast<uint16_t*>(m);
vec[0].len = 3;

uint16_t c = pcpp::computeChecksum(vec, 1);
Expand Down
2 changes: 1 addition & 1 deletion Tests/Packet++Test/Utils/TestUtils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ void savePacketToPcap(pcpp::Packet& packet, const std::string &fileName)
hdr.ts.tv_usec = 0; /* ms */
hdr.caplen = hdr.len = packet.getRawPacket()->getRawDataLen();
/* write single IP packet */
pcap_dump((u_char*)d, &hdr, packet.getRawPacketReadOnly()->getRawData());
pcap_dump(static_cast<u_char*>(d), &hdr, packet.getRawPacketReadOnly()->getRawData());

/* finish up */
pcap_dump_close(d);
Expand Down
4 changes: 2 additions & 2 deletions Tests/Packet++Test/Utils/TestUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ void testSetUp();

#define READ_FILE_AND_CREATE_PACKET(num, filename) \
READ_FILE_INTO_BUFFER(num, filename); \
pcpp::RawPacket rawPacket##num((const uint8_t*)buffer##num, bufferLength##num, time, true)
pcpp::RawPacket rawPacket##num(static_cast<const uint8_t*>(buffer##num), bufferLength##num, time, true)

#define READ_FILE_AND_CREATE_PACKET_LINKTYPE(num, filename, linktype) \
READ_FILE_INTO_BUFFER(num, filename); \
pcpp::RawPacket rawPacket##num((const uint8_t*)buffer##num, bufferLength##num, time, true, linktype)
pcpp::RawPacket rawPacket##num(static_cast<const uint8_t*>(buffer##num), bufferLength##num, time, true, linktype)

#ifdef PCPP_TESTS_DEBUG
void savePacketToPcap(pcpp::Packet& packet, const std::string &fileName);
Expand Down
4 changes: 2 additions & 2 deletions Tests/Pcap++Test/Tests/KniTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ struct KniRequestsCallbacksMock

static bool onPacketsCallbackSingleBurst(pcpp::MBufRawPacket*, uint32_t numOfPackets, pcpp::KniDevice*, void* userCookie)
{
unsigned int* counter = (unsigned int*)userCookie;
unsigned int* counter = static_cast<unsigned int*>(userCookie);
*counter = numOfPackets;
// Break after first burst
return false;
Expand All @@ -34,7 +34,7 @@ struct KniRequestsCallbacksMock
}
static bool onPacketsCallback(pcpp::MBufRawPacket*, uint32_t numOfPackets, pcpp::KniDevice*, void* userCookie)
{
unsigned int* counter = (unsigned int*)userCookie;
unsigned int* counter = static_cast<unsigned int*>(userCookie);
*counter = *counter + numOfPackets;
return true;
}
Expand Down
40 changes: 20 additions & 20 deletions Tests/Pcap++Test/Tests/LiveDeviceTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,12 @@ extern PcapTestArgs PcapTestGlobalArgs;

static void packetArrives(pcpp::RawPacket* rawPacket, pcpp::PcapLiveDevice* pDevice, void* userCookie)
{
(*(int*)userCookie)++;
(*static_cast<int*>(userCookie))++;
}

static void statsUpdate(pcpp::IPcapDevice::PcapStats& stats, void* userCookie)
{
(*(int*)userCookie)++;
(*static_cast<int*>(userCookie))++;
}

static bool packetArrivesBlockingModeTimeout(pcpp::RawPacket* rawPacket, pcpp::PcapLiveDevice* dev, void* userCookie)
Expand All @@ -37,7 +37,7 @@ static bool packetArrivesBlockingModeTimeout(pcpp::RawPacket* rawPacket, pcpp::P

static bool packetArrivesBlockingModeNoTimeout(pcpp::RawPacket* rawPacket, pcpp::PcapLiveDevice* dev, void* userCookie)
{
int* packetCount = (int*)userCookie;
int* packetCount = static_cast<int*>(userCookie);
if ((*packetCount) == 5)
return true;

Expand All @@ -57,7 +57,7 @@ static bool packetArrivesBlockingModeStartCapture(pcpp::RawPacket* rawPacket, pc

pcpp::Logger::getInstance().enableLogs();

int* packetCount = (int*)userCookie;
int *packetCount = static_cast<int*>(userCookie);
Dimi1010 marked this conversation as resolved.
Show resolved Hide resolved
if ((*packetCount) == 5)
return true;

Expand All @@ -70,7 +70,7 @@ static bool packetArrivesBlockingModeStopCapture(pcpp::RawPacket* rawPacket, pcp
// shouldn't do anything
dev->stopCapture();

int* packetCount = (int*)userCookie;
int* packetCount = static_cast<int*>(userCookie);
if ((*packetCount) == 5)
return true;

Expand All @@ -80,14 +80,14 @@ static bool packetArrivesBlockingModeStopCapture(pcpp::RawPacket* rawPacket, pcp

static bool packetArrivesBlockingModeNoTimeoutPacketCount(pcpp::RawPacket* rawPacket, pcpp::PcapLiveDevice* dev, void* userCookie)
{
int* packetCount = (int*)userCookie;
int *packetCount = static_cast<int*>(userCookie);
Dimi1010 marked this conversation as resolved.
Show resolved Hide resolved
(*packetCount)++;
return false;
}

static bool packetArrivesBlockingModeWithSnaplen(pcpp::RawPacket* rawPacket, pcpp::PcapLiveDevice* dev, void* userCookie)
{
int snaplen = *(int*)userCookie;
int snaplen = *static_cast<int*>(userCookie);
return rawPacket->getRawDataLen() > snaplen;
}

Expand Down Expand Up @@ -118,7 +118,7 @@ class RpcapdServerInitializer
if (!CreateProcess
(
TEXT(cmd.c_str()),
(char*)TEXT(args.str().c_str()),
const_cast<char*>(TEXT(args.str().c_str())), // TODO: This can potentially cause access violation if Unicode version CreateProcessW is chosen.
NULL,NULL,FALSE,
CREATE_NEW_CONSOLE,
NULL,NULL,
Expand Down Expand Up @@ -229,7 +229,7 @@ PTF_TEST_CASE(TestPcapLiveDevice)
DeviceTeardown devTeardown(liveDev);
int packetCount = 0;
int numOfTimeStatsWereInvoked = 0;
PTF_ASSERT_TRUE(liveDev->startCapture(&packetArrives, (void*)&packetCount, 1, &statsUpdate, (void*)&numOfTimeStatsWereInvoked));
PTF_ASSERT_TRUE(liveDev->startCapture(&packetArrives, static_cast<void*>(&packetCount), 1, &statsUpdate, static_cast<void*>(&numOfTimeStatsWereInvoked)));
int totalSleepTime = 0;
while (totalSleepTime <= 20)
{
Expand All @@ -254,7 +254,7 @@ PTF_TEST_CASE(TestPcapLiveDevice)

// a negative test
pcpp::Logger::getInstance().suppressLogs();
PTF_ASSERT_FALSE(liveDev->startCapture(&packetArrives, (void*)&packetCount, 1, &statsUpdate, (void*)&numOfTimeStatsWereInvoked));
PTF_ASSERT_FALSE(liveDev->startCapture(&packetArrives, static_cast<void*>(&packetCount), 1, &statsUpdate, static_cast<void*>(&numOfTimeStatsWereInvoked)));
pcpp::Logger::getInstance().enableLogs();
} // TestPcapLiveDevice

Expand All @@ -272,7 +272,7 @@ PTF_TEST_CASE(TestPcapLiveDeviceClone)
DeviceTeardown devTeardown(liveDev, true);
int packetCount = 0;
int numOfTimeStatsWereInvoked = 0;
PTF_ASSERT_TRUE(liveDev->startCapture(&packetArrives, (void*)&packetCount, 1, &statsUpdate, (void*)&numOfTimeStatsWereInvoked));
PTF_ASSERT_TRUE(liveDev->startCapture(&packetArrives, static_cast<void*>(&packetCount), 1, &statsUpdate, static_cast<void*>(&numOfTimeStatsWereInvoked)));
int totalSleepTime = 0;
while (totalSleepTime <= 20)
{
Expand All @@ -296,7 +296,7 @@ PTF_TEST_CASE(TestPcapLiveDeviceClone)

// a negative test
pcpp::Logger::getInstance().suppressLogs();
PTF_ASSERT_FALSE(liveDev->startCapture(&packetArrives, (void*)&packetCount, 1, &statsUpdate, (void*)&numOfTimeStatsWereInvoked));
PTF_ASSERT_FALSE(liveDev->startCapture(&packetArrives, static_cast<void*>(&packetCount), 1, &statsUpdate, static_cast<void*>(&numOfTimeStatsWereInvoked)));
pcpp::Logger::getInstance().enableLogs();

} // TestPcapLiveDeviceClone
Expand Down Expand Up @@ -343,7 +343,7 @@ PTF_TEST_CASE(TestPcapLiveDeviceStatsMode)
PTF_ASSERT_TRUE(liveDev->open());
DeviceTeardown devTeardown(liveDev);
int numOfTimeStatsWereInvoked = 0;
PTF_ASSERT_TRUE(liveDev->startCapture(1, &statsUpdate, (void*)&numOfTimeStatsWereInvoked));
PTF_ASSERT_TRUE(liveDev->startCapture(1, &statsUpdate, static_cast<void*>(&numOfTimeStatsWereInvoked)));
sendURLRequest("www.ebay.com");
int totalSleepTime = 0;
while (totalSleepTime <= 6)
Expand All @@ -370,7 +370,7 @@ PTF_TEST_CASE(TestPcapLiveDeviceStatsMode)

// a negative test
pcpp::Logger::getInstance().suppressLogs();
PTF_ASSERT_FALSE(liveDev->startCapture(1, &statsUpdate, (void*)&numOfTimeStatsWereInvoked));
PTF_ASSERT_FALSE(liveDev->startCapture(1, &statsUpdate, static_cast<void*>(&numOfTimeStatsWereInvoked)));
pcpp::Logger::getInstance().enableLogs();
} // TestPcapLiveDeviceStatsMode

Expand Down Expand Up @@ -491,15 +491,15 @@ PTF_TEST_CASE(TestPcapLiveDeviceWithLambda)

auto packetArrivesLambda = [](pcpp::RawPacket* rawPacket, pcpp::PcapLiveDevice* pDevice, void* userCookie)
{
(*(int*)userCookie)++;
(*static_cast<int*>(userCookie))++;
};

auto statsUpdateLambda = [](pcpp::IPcapDevice::PcapStats& stats, void* userCookie)
{
(*(int*)userCookie)++;
(*static_cast<int*>(userCookie))++;
};

PTF_ASSERT_TRUE(liveDev->startCapture(packetArrivesLambda , (void*)&packetCount, 1, statsUpdateLambda, (void*)&numOfTimeStatsWereInvoked));
PTF_ASSERT_TRUE(liveDev->startCapture(packetArrivesLambda , static_cast<void*>(&packetCount), 1, statsUpdateLambda, static_cast<void*>(&numOfTimeStatsWereInvoked)));
int totalSleepTime = 0;
while (totalSleepTime <= 20)
{
Expand All @@ -523,7 +523,7 @@ PTF_TEST_CASE(TestPcapLiveDeviceBlockingModeWithLambda)
auto packetArrivesBlockingModeNoTimeoutLambda = [](
pcpp::RawPacket *rawPacket, pcpp::PcapLiveDevice *dev, void *userCookie)
{
int *packetCount = (int *)userCookie;
int *packetCount = static_cast<int*>(userCookie);
if ((*packetCount) == 5)
return true;

Expand Down Expand Up @@ -632,7 +632,7 @@ PTF_TEST_CASE(TestWinPcapLiveDevice)
PTF_ASSERT_TRUE(winPcapLiveDevice->setMinAmountOfDataToCopyFromKernelToApplication(100000));
int packetCount = 0;
int numOfTimeStatsWereInvoked = 0;
PTF_ASSERT_TRUE(winPcapLiveDevice->startCapture(&packetArrives, (void*)&packetCount, 1, &statsUpdate, (void*)&numOfTimeStatsWereInvoked));
PTF_ASSERT_TRUE(winPcapLiveDevice->startCapture(&packetArrives, static_cast<void*>(&packetCount), 1, &statsUpdate, static_cast<void*>(&numOfTimeStatsWereInvoked)));
for (int i = 0; i < 5; i++)
{
sendURLRequest("www.ebay.com");
Expand All @@ -650,7 +650,7 @@ PTF_TEST_CASE(TestWinPcapLiveDevice)

// a negative test
pcpp::Logger::getInstance().suppressLogs();
PTF_ASSERT_FALSE(winPcapLiveDevice->startCapture(&packetArrives, (void*)&packetCount, 1, &statsUpdate, (void*)&numOfTimeStatsWereInvoked));
PTF_ASSERT_FALSE(winPcapLiveDevice->startCapture(&packetArrives, static_cast<void*>(&packetCount), 1, &statsUpdate, static_cast<void*>(&numOfTimeStatsWereInvoked)));
pcpp::Logger::getInstance().enableLogs();

#else
Expand Down
6 changes: 3 additions & 3 deletions Tests/Pcap++Test/Tests/PfRingTests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ struct SetFilterInstruction

static void pfRingPacketsArrive(pcpp::RawPacket* packets, uint32_t numOfPackets, uint8_t threadId, pcpp::PfRingDevice* device, void* userCookie)
{
PfRingPacketData* data = (PfRingPacketData*)userCookie;
PfRingPacketData* data = static_cast<PfRingPacketData*>(userCookie);

data->ThreadId = threadId;
data->PacketCount += numOfPackets;
Expand All @@ -64,7 +64,7 @@ static void pfRingPacketsArrive(pcpp::RawPacket* packets, uint32_t numOfPackets,

static void pfRingPacketsArriveMultiThread(pcpp::RawPacket* packets, uint32_t numOfPackets, uint8_t threadId, pcpp::PfRingDevice* device, void* userCookie)
{
PfRingPacketData* data = (PfRingPacketData*)userCookie;
PfRingPacketData* data = static_cast<PfRingPacketData*>(userCookie);

data[threadId].ThreadId = threadId;
data[threadId].PacketCount += numOfPackets;
Expand Down Expand Up @@ -93,7 +93,7 @@ static void pfRingPacketsArriveMultiThread(pcpp::RawPacket* packets, uint32_t nu

void pfRingPacketsArriveSetFilter(pcpp::RawPacket* packets, uint32_t numOfPackets, uint8_t threadId, pcpp::PfRingDevice* device, void* userCookie)
{
SetFilterInstruction* instruction = (SetFilterInstruction*)userCookie;
SetFilterInstruction* instruction = static_cast<SetFilterInstruction*>(userCookie);
switch(instruction->Instruction)
{
case 1: //verify TCP packet
Expand Down
Loading