npcap icon indicating copy to clipboard operation
npcap copied to clipboard

Crash when pcap_freealldevs called

Open 66demon666 opened this issue 1 year ago • 8 comments

The gist is in the title - when I call pcap_freealldevs the program crashes without explanation. All other functions work fine. This fact does not hinder development much, but still. I use Delphi, can anyone confirm the presence of this situation in other languages ​​to narrow down the problem?

  PPcap_if = ^TPcap_if;

  TPcap_if = packed record
    next: PPcap_if;
    name: PAnsiChar;
    description: PAnsiChar;
    addresses: PPcap_addr;
    flags: Cardinal;
  end;

procedure pcap_freealldevs(pcap_if_t: PPcap_if); stdcall; external 'wpcap.dll';```

66demon666 avatar Mar 01 '25 14:03 66demon666

stdcall; ?? Where did you get that idea? It's cdecl.

gvanem avatar Mar 01 '25 14:03 gvanem

stdcall; ?? Where did you get that idea? It's cdecl.

Yes, libpcap is written in C, originally for UN*Xes, for which the C calling sequence is the standard, or the only, calling sequence, and, when used in WinPcap, they didn't add _stdcall or anything else to make it use a non-default calling sequence, so it defaults to _cdecl. Npcap didn't change that.

A Google search for

Delphi WinPcap

found https://github.com/den-kozlov/Snoop; if you look at https://github.com/den-kozlov/Snoop/blob/master/Pcap.pas, you will note that it uses cdecl.

Searching for Npcap rather than WinPcap (and quoting "Delphi" to force Google not to return unhelpful results that don't mention Delphi) also found https://www.magsys.co.uk/delphi/magmonsock.asp and https://cdn.nsoftware.com/help/IPH/dlp/IPMonitor.htm.

So don't invent your own wrapper descriptions without, for example, first looking at files such as https://github.com/den-kozlov/Snoop/blob/master/Pcap.pas, and Do. Not. Use. stdcall.

That may or may not be the cause of your crashes, but it should be fixed before you go any further.

guyharris avatar Mar 01 '25 17:03 guyharris

Yea, i was playing with stdcall and cdecl, but result not change :(

66demon666 avatar Mar 01 '25 23:03 66demon666

Yea, i was playing with stdcall and cdecl, but result not change :(

So you fixed all of the declarations, and recompiled everything, and it still crashes?

If so, please show us the pcap_findalldevs() call that allocated the array of TPcap_if structures, the pcap_freealldevs() call that was intended to free that array, and all the code between them.

(And, while you're at it, edit the initial bug report so that the first ` at the beginning of the code sample is replaced by three ` on a row by themselves and the last ` at the end of the code is replaced by three ` on a row by themselves; that's the way to make a block of code show up as fixed-width text.)

guyharris avatar Mar 01 '25 23:03 guyharris

Okay guys, I quickly threw together a new project with the same types to simplify things. The crash remains, here are the data structures. So, until I call pcap_freealldevs everything is ok, but right after I write this line, the program crashes. Even try catch doesn't stop it

PPPcap_if = ^PPcap_if;
  PPcap_if = ^TPcap_if;

  TPcap_if = packed record
    next: PPcap_if;
    name: PAnsiChar;
    description: PAnsiChar;
    addresses: PPcap_addr;
    flags: Cardinal;
  end;

IF_ID = '\Device\NPF_{077C8EF5-CB84-4C34-9C2A-66006D41D835}';

function pcap_findalldevs_ex(source: PAnsiChar; auth: PPcap_rmtauth;
  addresses: PPPcap_if; errbuf: PAnsiChar): integer; cdecl;
  external 'wpcap.dll';

procedure pcap_freealldevs(pcap_if_t: PPcap_if); cdecl; external 'wpcap.dll';

var
AllDevices: TPcap_if;

 if pcap_findalldevs_ex(IF_ID, nil, @AllDevices, @errbuf) = -1 then
    writeln('Findalldevs error')
  else
    writeln('findalldevs success');
  pcap_freealldevs(@AllDevices);
  readln;

66demon666 avatar Mar 02 '25 11:03 66demon666

AllDevices: TPcap_if;

No, it should be a PPcap_if. From pcap_findalldevs() and pcap_findalldevs_ex() you don't get a structure, you get a pointer to a structure that's the first element in a list of those structures.

if pcap_findalldevs_ex(IF_ID, nil, @AllDevices, @errbuf) = -1 then
    writeln('Findalldevs error')
  else
    writeln('findalldevs success');

That part is mostly correct - the third argument to pcap_findalldevs_ex() is a pointer to a variable that is a pointer to a structure, and if AllDevices is changed to a PPcap_if, that will be correct.

pcap_freealldevs(@AllDevices);

Once AllDevices is changed to a PPcap_if, that line should be changed to

 pcap_freealldevs(AllDevices);

i.e., pass it a pointer to the first element of the list of structures, and it will free the entire list.

guyharris avatar Mar 02 '25 17:03 guyharris

damn, I'm not new to Delphi, but working with pointers here is a bit... Not obvious. Thanks a lot!

66demon666 avatar Mar 02 '25 18:03 66demon666

In standard Pascal, it appears the only way to get a pointer value is to use New(). You cannot, for example, construct a pointer that points to what Pascal calls a "static" variable" (which includes block-local variables).

I'm guessing the @ operator is an extension in Turbo Pascal/Delphi, and allows a pointer value that points to a local variable to be obtained.

C has no such restrictions; the & operator is a part of standard C, and C APIs often return values through pointers. For example, pcap_findalldevs() returns both a status value and, if the status value indicates success, a pointer to the first element of a list of devices on which you can capture. That style of API may be less common in Delphi APIs, and Delphi programmers may find that style of programming a bit alien.

guyharris avatar Mar 03 '25 07:03 guyharris