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

Simplified allocation of heap buffer during gateway fetch on win32. #1402

Merged
merged 4 commits into from
May 25, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 9 additions & 10 deletions Pcap++/src/PcapLiveDevice.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
#include <fstream>
#include <chrono>
#include <sstream>
#include <vector>
#if defined(_WIN32)
// The definition of BPF_MAJOR_VERSION is required to support Npcap. In Npcap there are
// compilation errors due to struct redefinition when including both Packet32.h and pcap.h
Expand Down Expand Up @@ -982,16 +983,18 @@ void PcapLiveDevice::setDefaultGateway()
{
#if defined(_WIN32)
ULONG outBufLen = sizeof (IP_ADAPTER_INFO);
uint8_t* buffer = new uint8_t[outBufLen];
PIP_ADAPTER_INFO adapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(buffer);
std::vector<uint8_t> buffer(outBufLen);
PIP_ADAPTER_INFO adapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(buffer.data());
DWORD retVal = 0;

retVal = GetAdaptersInfo(adapterInfo, &outBufLen);
uint8_t* buffer2 = new uint8_t[outBufLen];
if (retVal == ERROR_BUFFER_OVERFLOW)
adapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(buffer2);

retVal = GetAdaptersInfo(adapterInfo, &outBufLen);
{
buffer.resize(outBufLen);
// Repins the adapter info pointer to the vector data pointer as the vector might be reallocated during the resize.
adapterInfo = reinterpret_cast<IP_ADAPTER_INFO*>(buffer.data());
retVal = GetAdaptersInfo(adapterInfo, &outBufLen);
}

if (retVal == NO_ERROR)
{
Expand All @@ -1018,10 +1021,6 @@ void PcapLiveDevice::setDefaultGateway()
{
PCPP_LOG_ERROR("Error retrieving default gateway address");
}

delete[] buffer;
// cppcheck-suppress uninitdata
delete[] buffer2;
#elif defined(__linux__)
std::ifstream routeFile("/proc/net/route");
std::string line;
Expand Down
Loading