Skip to content

Building with clang-cl #625

@gvanem

Description

@gvanem

When using clang-cl to compile the NPcap sources, there are some C++ errors that MSVC's compiler
does not even warn about even on -W4. Like this:

BOOLEAN PacketGetNetInfoEx(PCCH AdapterName, npf_if_addr* buffer, PLONG NEntries)
{
   ...
	if (!AdapterName || !buffer || NEntries <= 0) {   // !! here

Error:

./NPcap/packetWin7/Dll/Packet32.cpp(3220,42): error: ordered comparison between pointer and zero ('PLONG' (aka 'long *') and 'int')
        if (!AdapterName || !buffer || NEntries <= 0) {
                                       ~~~~~~~~ ^  ~

Surely it should be:

	if (!AdapterName || !buffer || *NEntries <= 0) {

A bug that surfaces depending on the raw location of *NEntries.

And then there are these errors:

./NPcap/packetWin7/Dll/Packet32.cpp(3672,8): error: case value evaluates to -1073676267, which cannot be narrowed to type 'DWORD'
      (aka 'unsigned long') [-Wc++11-narrowing]
                case NDIS_STATUS_INVALID_DATA:
                     ^
./NPcap/packetWin7/Dll/Packet32.cpp(3663,49): note: expanded from macro 'NDIS_STATUS_INVALID_DATA'
#define NDIS_STATUS_INVALID_DATA                ((NDIS_STATUS)0xC0010015L)
                                                ^
./NPcap/packetWin7/Dll/Packet32.cpp(3673,8): error: case value evaluates to -1073676265, which cannot be narrowed to type 'DWORD'
      (aka 'unsigned long') [-Wc++11-narrowing]
                case NDIS_STATUS_INVALID_OID:
                     ^
./NPcap/packetWin7/Dll/Packet32.cpp(3664,49): note: expanded from macro 'NDIS_STATUS_INVALID_OID'
#define NDIS_STATUS_INVALID_OID                 ((NDIS_STATUS)0xC0010017L)
                                                ^

Since NDIS_STATUS_INVALID_DATA == (NDIS_STATUS)0xC0010015L expands to (int) -1073676267 that's not suitable for a DWORD.
But with this patch, it compiles:

--- a/NPcap/packetWin7/Dll/Packet32.cpp 2022-08-20 12:20:53
+++ b/NPcap/packetWin7/Dll/Packet32.cpp 2022-08-20 08:27:46
@@ -3660,8 +3660,8 @@
        dwResult = PacketRequestHelper(hAdapter, TRUE, OidData);

 #ifndef NDIS_STATUS_INVALID_DATA
-#define NDIS_STATUS_INVALID_DATA                ((DWORD)0xC0010015UL)
-#define NDIS_STATUS_INVALID_OID                 ((DWORD)0xC0010017UL)
+#define NDIS_STATUS_INVALID_DATA                ((NDIS_STATUS)0xC0010015L)
+#define NDIS_STATUS_INVALID_OID                 ((NDIS_STATUS)0xC0010017L)
 #endif
        switch (dwResult)
        {

Or adding -Wno-c++11-narrowing.

I've not yet tried to use clang-cl for the driver code itself. I suspect more such issues there.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions