npcap icon indicating copy to clipboard operation
npcap copied to clipboard

Compiling 'Packet32.cpp' with '-std:c++20'

Open gvanem opened this issue 1 year ago • 2 comments

The default C++ standard in cl is C++14. And using that for packetWin7/Dll/Packet32.cpp works fine. But using -std:c++20 or -std:c++latest does not. This is what I did:

cd packetWin7\Dll\Project
set CL=-std:c++20
msbuild -nologo -p:Configuration=Release -p:Platform="x64" Packet.sln

The errors generated by MSVC version 19.42.34321.1 for x64 (the latest):

Packet32.cpp(414): error C2362: initialization of 'nFields' is skipped by 'goto Exit'
Packet32.cpp(422): note: see declaration of 'Exit'
Packet32.cpp(3369): error C2362: initialization of 'numEntries' is skipped by 'goto END_PacketGetNetInfoEx'
Packet32.cpp(3417): note: see declaration of 'END_PacketGetNetInfoEx'
Packet32.cpp(3368): error C2362: initialization of 'pAddr' is skipped by 'goto END_PacketGetNetInfoEx'
Packet32.cpp(3417): note: see declaration of 'END_PacketGetNetInfoEx'
Packet32.cpp(3314): error C2362: initialization of 'RetVal' is skipped by 'goto END_PacketGetNetInfoEx'
Packet32.cpp(3417): note: see declaration of 'END_PacketGetNetInfoEx'
Packet32.cpp(3301): error C2362: initialization of 'AdapterGuid' is skipped by 'goto END_PacketGetNetInfoEx'
Packet32.cpp(3417): note: see declaration of 'END_PacketGetNetInfoEx'

Googling this issue seems to be related to option /Za which is not in use.

And further, using -std:c++20 or -std:c++latest with clang-cl ver. 18.1 works just fine. So I'm not sure what is the real issue with the Packet32.cpp code.

gvanem avatar Sep 26 '24 10:09 gvanem

So I'm not sure what is the real issue with the Packet32.cpp code.

A fix for this was to move some variables to the top:

--- a/Packet32.cpp 2024-08-04 08:43:33
+++ b/Packet32.cpp 2024-09-26 12:30:29
@@ -347,6 +347,7 @@
        BOOL   fSuccess = FALSE;
        DWORD  cbRead, cbToWrite, cbWritten, dwMode;
        HANDLE hPipe = g_hNpcapHelperPipe;
+       int nFields;

        TRACE_ENTER();

@@ -411,7 +412,7 @@
                goto Exit;
        }

-       int nFields = _snscanf_s(chBuf, cbRead, "%p,%lu", &hd, pdwError);
+       nFields = _snscanf_s(chBuf, cbRead, "%p,%lu", &hd, pdwError);
        if (nFields != 2)
        {
                *pdwError = ERROR_OPEN_FAILED;
@@ -3218,6 +3219,10 @@
        PCHAR Tname = NULL;
        BOOLEAN Res = FALSE;
        DWORD err = ERROR_SUCCESS;
+       LONG numEntries;
+       PIP_ADAPTER_UNICAST_ADDRESS pAddr;
+       PCCH AdapterGuid;
+       ULONG RetVal;

        TRACE_ENTER();

@@ -3298,7 +3303,7 @@
                goto END_PacketGetNetInfoEx;
        }

-       PCCH AdapterGuid = strchr(AdapterName, '{');
+       AdapterGuid = strchr(AdapterName, '{');
        if (AdapterGuid == NULL)
        {
                err = ERROR_INVALID_NAME;
@@ -3311,7 +3316,7 @@
                err = ERROR_NOT_ENOUGH_MEMORY;
                goto END_PacketGetNetInfoEx;
        }
-       ULONG RetVal = ERROR_SUCCESS;
+       RetVal = ERROR_SUCCESS;
        for (int i = 0; i < ADAPTERS_ADDRESSES_MAX_TRIES; i++)
        {

@@ -3365,8 +3370,8 @@

        // else found!
        Res = TRUE;
-       PIP_ADAPTER_UNICAST_ADDRESS pAddr = TmpAddr->FirstUnicastAddress;
-       LONG numEntries = 0;
+       pAddr = TmpAddr->FirstUnicastAddress;
+       numEntries = 0;
        while (pAddr != NULL && numEntries < *NEntries)
        {
                ULONG ul = 0;

gvanem avatar Sep 26 '24 10:09 gvanem

Thanks for pointing this out. The function in question doesn't have any resulting bugs, since the Exit: label is at the end of the function and none of the uninitialized variables are referenced past that point, but I do see that this could result in bugs down the road if the function were to be changed. I will fix this when I have some time.

dmiller-nmap avatar Oct 31 '24 16:10 dmiller-nmap