-
Notifications
You must be signed in to change notification settings - Fork 8
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
Possible bug in freehostent()
#115
Comments
That is a very weird loop. Might be a good idea to see what ccc-analyzer and memcheck report |
Why don't you just use an incremented index, e.g.: diff --git a/src/get_ip.c b/src/get_ip.c
index dd039c1..196f751 100644
--- a/src/get_ip.c
+++ b/src/get_ip.c
@@ -238,7 +238,7 @@ ret_copy:
void W32_CALL freehostent (struct hostent *he)
{
- char *p;
+ int idx;
SOCK_DEBUGF (("\nfreehostent: %s ", he->h_name));
@@ -248,11 +248,11 @@ void W32_CALL freehostent (struct hostent *he)
free (he->h_name);
he->h_name = NULL;
- for (p = he->h_addr_list[0]; p; p++)
- free (p);
+ for (idx = 0; he->h_addr_list[idx]; ++idx)
+ free (he->h_addr_list[idx]);
- for (p = he->h_aliases[0]; p; p++)
- free (p);
+ for (idx = 0; he->h_aliases[idx]; ++idx)
+ free (he->h_aliases[idx]);
free (he->h_aliases);
free (he->h_addr_list); |
That code must be ~15 years old. But no code in Watt-32 is calling it AFAICS. |
Agree, much easier to read.
I think this never showed up because compilers already identified the bug, and optimized the whole loop away. They just didn't emit a warning about it. Test case:
Would be curious to see what static analysis / sanitizers have to say about it. |
I've committed a fix for this in dca78e7. |
The following test code freezes when /*
Compile with:
wcl -ml -i=inc test.c lib\wattcpwl.lib
wcl386 -mf -i=inc test.c lib\wattcpwf.lib
*/
#include <stdio.h>
#include <malloc.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>
void heap_dump() {
struct _heapinfo h_info;
int heap_status, it = 0;
h_info._pentry = NULL;
for(;;) {
heap_status = _heapwalk( &h_info );
if( heap_status != _HEAPOK ) break;
printf( " %s block at %Fp of size %4.4X\n",
(h_info._useflag == _USEDENTRY ? "USED" : "FREE"),
h_info._pentry, h_info._size );
++it;
}
switch( heap_status ) {
case _HEAPEND:
printf( "OK - end of heap - %d blocks\n", it);
break;
case _HEAPEMPTY:
printf( "OK - heap is empty\n" );
break;
case _HEAPBADBEGIN:
printf( "ERROR - heap is damaged\n" );
break;
case _HEAPBADPTR:
printf( "ERROR - bad pointer to heap\n" );
break;
case _HEAPBADNODE:
printf( "ERROR - bad node in heap\n" );
}
}
int main() {
struct hostent *host_info;
struct in_addr addr;
puts("Startup");
heap_dump();
addr.s_addr = inet_addr("127.0.0.1");
if(!(host_info = gethostbyaddr((char *)&addr, sizeof(addr), AF_INET))) {
puts("Couldn't get hostinfo");
return 1;
}
puts("gethostbyaddr() success");
heap_dump();
freehostent(host_info);
puts("freehostent() success");
heap_dump();
return 0;
} |
Checking this with clang-cl + ASAN on Windows, I get: ...
OK - end of heap - 936 blocks
=================================================================
==10132==ERROR: AddressSanitizer: attempting free on address which was not malloc()-ed: 0x7ffc15459940 in thread T0
#0 0x7ffc0bd899c8 in free D:\a\_work\1\s\src\vctools\asan\llvm\compiler-rt\lib\asan\asan_malloc_win.cpp:248
#1 0x7ffc15217d4a in freehostent E:\WATT\src\get_ip.c:250
#2 0x7ff78c511503 in main E:\WATT\bin\test-freehostent.c:63
#3 0x7ff78c52b4a7 in invoke_main D:\a\_work\1\s\src\vctools\crt\vcstartup\src\startup\exe_common.inl:78
... |
Seems the cause is simply that |
Can Watt-32 be built on Linux for Linux? If so I'd be more than happy to run this and other test code through Valgrind |
for Linux does not make sense currently since Watt-32 would need some special feature ( |
If I'm not mistaken doesn't the Windows version of Watt-32 requires Npcap to function? If so I do agree that Watt-32 on Linux is kinda pointless for an actual application since Linux already has a great Berkeley socket implementation but for testing and debugging its rich C eco-system would be fantastic. As an example: I build a CMocka unit testing framework binary with |
Yes. WinPcap or NPcap (and SwsVpkt for Win-XP). But adding libpcap would be a massive change. |
Tried building with gcc 14 today. It produces some new warnings, mostly bogus (swapped
calloc()
arguments). But this one looks valid:I think this should be:
The text was updated successfully, but these errors were encountered: