为什么这个realloc()会失败?

时间:2015-03-19 04:12:07

标签: c http client realloc

我的第一个C项目是一个简单的类似cURL的HTTP客户端,它检索发送到服务器的请求的标头,内容和状态。它到目前为止一直在努力,但它还没有完成,我需要一些帮助。内存分配对我来说是一个障碍,而这个当前的问题是我自己无法解决的问题。

在名为HTTPresponse *requestHTTPresponse(URLdata *requestDestination)的函数中,由于某种未知原因,重新分配包含原始HTTP响应头和内容的字符串失败。原始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;
}

1 个答案:

答案 0 :(得分:1)

确保您的变量已初始化。在原始问题中,您尝试重新分配空指针,并且需要确保将responselength变量初始化为0.

-----编辑-----

此外,从网络连接中获取数据时,如果使用strcpy等函数,请确保字符串为零。