1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169
|
/*
*
* Stripped code sample to test a bug
* Files: 1
* - main.cpp
*
*/
/*
The program hangs at freeaddrinfo about 50% of the time
The program crashes at freeaddrinfo about 40% of the time
The program runs successfully 10% of the time
Tested on Windows 7(64-bit), compiled with minGW
*/
/*
Compilescript: Notepad++
SET g++ = C:\Dev-Cpp\bin\g++.exe
SET obj = $(CURRENT_DIRECTORY)\$(NAME_PART)
"$(g++)" -c "$(FULL_CURRENT_PATH)" -o "$(obj).o"
"$(g++)" "$(obj).o" -lalleg -lkernel32 C:/Dev-Cpp/lib/libws2_32.a -luser32 -lgdi32 -lcomdlg32 -lole32 -ldinput -lddraw -ldxguid -lwinmm -ldsound -o "$(obj).exe"
UNSET obj
UNSET g++
*/
#include <windows.h>
#include <stdio.h>
#include <Ws2tcpip.h>
void error(char* szExitMessage);
SOCKET connectWalk(struct addrinfo* ai_SocketInfo);
int tcpConnect(char* host, char* port, SOCKET* connection);
using namespace std;
int main(){
//Initialize WSAData
int nStatus = 0;
WSADATA wsadata;
printf("Starting WSAData...\r\n");
nStatus = WSAStartup(MAKEWORD(2,2),&wsadata);
if(nStatus != NO_ERROR){
error("Error at WSAData\r\n");
}
//Connect part
char* szHost = "www.google.no";
char* szPort = "80";
SOCKET sDownloadSock;
nStatus = tcpConnect(szHost, szPort, &sDownloadSock);
printf("Connected Successfully, lets try to send something.");
char szTest[512] = "";
sprintf(szTest, "GET /%s HTTP/1.1\r\nHost: %s\r\n\r\n", "", szHost);
nStatus = send(sDownloadSock, szTest, strlen(szTest), 0);
printf("Debugging: \r\n\tsend() / nStatus = %d\r\n\tWSAGetLastError() = %d\r\n", nStatus, WSAGetLastError());
error("Success!");
}
void error(char* szExitMessage){
printf("%s\r\n", szExitMessage);
system("pause");
exit(1);
}
int tcpConnect(char* host, char* port, SOCKET* connection){
printf("tcpConnect: Connecting to host: %s:%s.\r\n", host, port);
int status;
struct addrinfo setupInfo, *tempDeleter;
struct addrinfo* retInfo;
tempDeleter = &setupInfo;
memset(&setupInfo, 0, sizeof(setupInfo));
setupInfo.ai_family = AF_UNSPEC;
setupInfo.ai_socktype = SOCK_STREAM;
setupInfo.ai_flags = AI_PASSIVE;
status = getaddrinfo(host, port, &setupInfo, &retInfo);
if(status != 0){
printf("Failed @ getaddrinfo![%d//%d]", status, WSAGetLastError());
freeaddrinfo(&setupInfo);
freeaddrinfo(retInfo);
*connection = INVALID_SOCKET;
error("failed after getaddrinfo");
}
*connection = connectWalk(retInfo);
printf("freeing addrinfo's\r\n");
//This is where the program hangs
// It starts hanging here about 50% of the time.
freeaddrinfo(retInfo);
freeaddrinfo(tempDeleter);
//end of crash place 1
printf("freeaddrinfo() done, tcpConnect done.");
if(*connection == INVALID_SOCKET){
error("Failed when connecting to socket");
closesocket(*connection);
*connection = INVALID_SOCKET;
return 1;
}
return 0;
}
SOCKET connectWalk(struct addrinfo* ai_SocketInfo){
struct addrinfo* tryInfo;
SOCKET retSock;
for(tryInfo = ai_SocketInfo; tryInfo != NULL; tryInfo = tryInfo->ai_next){ //walk through sockets and connect to first available one
if((retSock = socket(tryInfo->ai_family, tryInfo->ai_socktype, tryInfo->ai_protocol)) == -1){
#ifdef PRINTSOCKERRORS
log("WALKERROR: socket");
#endif
continue;
}
if(connect(retSock, tryInfo->ai_addr, tryInfo->ai_addrlen) == -1){
closesocket(retSock);
#ifdef PRINTSOCKERRORS
log("WALKERROR: @connect\r\n");
#endif
continue;
}
break;
}
if(tryInfo == NULL){
closesocket(retSock);
#ifdef PRINTSOCKERRORS
log("WALKERROR: could not connect!");
#endif
freeaddrinfo(ai_SocketInfo);
return INVALID_SOCKET;
}
freeaddrinfo(ai_SocketInfo);
return retSock;
}
|