为什么这个 realloc() 会失败?
Why does this realloc() fail?
我的第一个 C 项目是一个简单的 cURL-like HTTP 客户端,它检索发送到服务器的请求的 headers、内容和状态。到目前为止一直在努力,但还没有完成,我需要一些帮助。内存分配对我来说是一个障碍,目前这个问题是我自己无法解决的。
在一个名为 HTTPresponse *requestHTTPresponse(URLdata *requestDestination)
的函数中,重新分配包含原始 HTTP 响应 headers 和内容的字符串由于某些未知原因而失败。原始 HTTP 响应字符串的指针设置为 NULL,我被迫退出我的程序。 为什么会发生这种行为,我该如何解决?此外,当我编译我的程序时,它有时会冻结并拒绝按预期打印文本或退出,这可能是DNS 挂起或其他一些奇怪的网络问题的结果。也欢迎任何其他建议。
可以找到完整的 C 源代码 here。
HTTPresponse *requestHTTPresponse(URLdata *requestDestination) {
char ip[INET6_ADDRSTRLEN], *request, *response = malloc(1), *headerTerminator = NULL, *responseTerminator = NULL, *headerString, *contentString, buffer[100];
int sockfd, bufferLength, responseLength;
struct addrinfo hints, *servinfo, *p;
/* Initialize HTTP response structure */
HTTPresponse *returnresponse = malloc(sizeof(HTTPresponse));
returnresponse->status = 0;
returnresponse->headers = NULL;
returnresponse->content = NULL;
/* Retrieve IP address of host */
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(requestDestination->host, requestDestination->port, &hints, &servinfo) != 0) {
return returnresponse;
}
/* Loop through all possible connections */
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
continue;
}
break;
}
if (p == NULL) {
return returnresponse;
}
/* Print IP address & free server information */
inet_ntop(p->ai_family, get_in_addr((struct sockaddr*)p->ai_addr), ip, sizeof(ip));
//printf("The IP address is: %s\n", ip);
freeaddrinfo(servinfo);
/* Format HTTP request */
request = malloc(27 + (int)strlen(requestDestination->path) + (int)strlen(requestDestination->host) * sizeof(char));
if (request == NULL) {
printf("Failure to allocate memory.\n");
exit(1);
}
sprintf(request, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", (*(requestDestination->path) == 0)?"/":requestDestination->path, requestDestination->host);
/* Send HTTP request */
send(sockfd, request, 26 + strlen(requestDestination->path) + strlen(requestDestination->host), 0);
free(request);
/* Receive HTTP response */
do {
bufferLength = recv(sockfd, buffer, 99, 0);
if (bufferLength == -1) {
break;
}
responseLength += bufferLength;
response = realloc(response, (responseLength + 1) * sizeof(char)); //Source of issues
if (response == NULL) {
printf("Failure to allocate memory.\n");
exit(2);
}
strncpy(response + responseLength - bufferLength, buffer, bufferLength);
if (headerTerminator) {
responseTerminator = strstr(response, "\r\n\r\n");
} else {
headerTerminator = strstr(response, "\r\n\r\n");
}
} while (responseTerminator == NULL);
/* Store data in structs */
headerString = malloc((headerTerminator - response + 1) * sizeof(char));
if (headerString == NULL) {
printf("Failure to allocate memory.\n");
exit(1);
}
strncpy(headerString, response, headerTerminator - response);
contentString = malloc((responseTerminator - headerTerminator) * sizeof(char));
if (contentString == NULL) {
printf("Failure to allocate memory.\n");
exit(1);
}
strncpy(contentString, headerTerminator, responseTerminator - headerTerminator);
returnresponse->headers = generateHeaderList(headerString);
returnresponse->content = contentString;
free(response);
/* Close connection & return HTTP response */
close(sockfd);
return returnresponse;
}
确保您的变量已初始化。在最初的问题中,您试图重新分配一个空指针,并且您需要确保将您的 responselength 变量初始化为 0。
----- 编辑 -----
此外,当从网络连接中获取数据时,如果您使用 strcpy 等函数,请确保您的字符串以零结尾。
我的第一个 C 项目是一个简单的 cURL-like HTTP 客户端,它检索发送到服务器的请求的 headers、内容和状态。到目前为止一直在努力,但还没有完成,我需要一些帮助。内存分配对我来说是一个障碍,目前这个问题是我自己无法解决的。
在一个名为 HTTPresponse *requestHTTPresponse(URLdata *requestDestination)
的函数中,重新分配包含原始 HTTP 响应 headers 和内容的字符串由于某些未知原因而失败。原始 HTTP 响应字符串的指针设置为 NULL,我被迫退出我的程序。 为什么会发生这种行为,我该如何解决?此外,当我编译我的程序时,它有时会冻结并拒绝按预期打印文本或退出,这可能是DNS 挂起或其他一些奇怪的网络问题的结果。也欢迎任何其他建议。
可以找到完整的 C 源代码 here。
HTTPresponse *requestHTTPresponse(URLdata *requestDestination) {
char ip[INET6_ADDRSTRLEN], *request, *response = malloc(1), *headerTerminator = NULL, *responseTerminator = NULL, *headerString, *contentString, buffer[100];
int sockfd, bufferLength, responseLength;
struct addrinfo hints, *servinfo, *p;
/* Initialize HTTP response structure */
HTTPresponse *returnresponse = malloc(sizeof(HTTPresponse));
returnresponse->status = 0;
returnresponse->headers = NULL;
returnresponse->content = NULL;
/* Retrieve IP address of host */
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
if (getaddrinfo(requestDestination->host, requestDestination->port, &hints, &servinfo) != 0) {
return returnresponse;
}
/* Loop through all possible connections */
for (p = servinfo; p != NULL; p = p->ai_next) {
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
continue;
}
if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) {
close(sockfd);
continue;
}
break;
}
if (p == NULL) {
return returnresponse;
}
/* Print IP address & free server information */
inet_ntop(p->ai_family, get_in_addr((struct sockaddr*)p->ai_addr), ip, sizeof(ip));
//printf("The IP address is: %s\n", ip);
freeaddrinfo(servinfo);
/* Format HTTP request */
request = malloc(27 + (int)strlen(requestDestination->path) + (int)strlen(requestDestination->host) * sizeof(char));
if (request == NULL) {
printf("Failure to allocate memory.\n");
exit(1);
}
sprintf(request, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", (*(requestDestination->path) == 0)?"/":requestDestination->path, requestDestination->host);
/* Send HTTP request */
send(sockfd, request, 26 + strlen(requestDestination->path) + strlen(requestDestination->host), 0);
free(request);
/* Receive HTTP response */
do {
bufferLength = recv(sockfd, buffer, 99, 0);
if (bufferLength == -1) {
break;
}
responseLength += bufferLength;
response = realloc(response, (responseLength + 1) * sizeof(char)); //Source of issues
if (response == NULL) {
printf("Failure to allocate memory.\n");
exit(2);
}
strncpy(response + responseLength - bufferLength, buffer, bufferLength);
if (headerTerminator) {
responseTerminator = strstr(response, "\r\n\r\n");
} else {
headerTerminator = strstr(response, "\r\n\r\n");
}
} while (responseTerminator == NULL);
/* Store data in structs */
headerString = malloc((headerTerminator - response + 1) * sizeof(char));
if (headerString == NULL) {
printf("Failure to allocate memory.\n");
exit(1);
}
strncpy(headerString, response, headerTerminator - response);
contentString = malloc((responseTerminator - headerTerminator) * sizeof(char));
if (contentString == NULL) {
printf("Failure to allocate memory.\n");
exit(1);
}
strncpy(contentString, headerTerminator, responseTerminator - headerTerminator);
returnresponse->headers = generateHeaderList(headerString);
returnresponse->content = contentString;
free(response);
/* Close connection & return HTTP response */
close(sockfd);
return returnresponse;
}
确保您的变量已初始化。在最初的问题中,您试图重新分配一个空指针,并且您需要确保将您的 responselength 变量初始化为 0。
----- 编辑 -----
此外,当从网络连接中获取数据时,如果您使用 strcpy 等函数,请确保您的字符串以零结尾。