帶有free()的HTTP客戶端:釋放char **內容時出現無效指針錯誤

[英]HTTP client with free(): invalid pointer error while freeing contents of char **


I've looked all over for solutions to this problem but tools like Valgrind and GDB are not helping me. Valgrind doesn't even finish running the program and GDB just tells me information that contradicts itself. I have an HTTP client that allocates a list of strings from the headers retrieved from a web server. When trying to free this allocated list of individually allocated strings, it decides to call free(): invalid pointer after I tried fixing the issue of the header-parsing function cutting off the final header. I've been looking at it for hours and I cannot find a single issue.

我已經全神貫注地尋找這個問題的解決方案,但像Valgrind和GDB這樣的工具並沒有幫助我。 Valgrind甚至沒有完成程序的運行,而GDB只是告訴我與之相矛盾的信息。我有一個HTTP客戶端,它從Web服務器檢索的頭中分配一個字符串列表。當試圖釋放這個單獨分配的字符串的已分配列表時,它決定調用free():無效指針后,我嘗試修復標題解析函數的問題,切斷最終標題。我一直在看它幾個小時,我找不到一個問題。

To be sure the code works, I've tested it with two main sites: http://www.google.com/ and http://en.wikipedia.org/. Usually only one works at a time when I fix a memory error on the other example.

為了確保代碼有效,我已經在兩個主要網站上進行了測試:http://www.google.com/和http://en.wikipedia.org/。通常只有一個在我修復另一個例子的內存錯誤時工作。

Here is where I parse the header string (a string containing \r\n between headers and only a terminating null at the end):

這是我解析頭字符串的地方(在頭之間包含\ r \ n的字符串,最后只有一個終止的null):

char **generateHeaderList(char *headers) {
    char **returnHeaders = NULL, *nextLine;
    int len, n;
    for (n = 0; (nextLine = strstr(headers, "\r\n")) != NULL; n++) {
        len = nextLine - headers;
        if (len == 0) break;
        returnHeaders = realloc(returnHeaders, (n + 1) * sizeof(char *));
        if (returnHeaders == NULL) {
            printf("Failure to allocate memory at line %d.\r\n", __LINE__ - 2);
            exit(1);
        }
        returnHeaders[n] = malloc((len + 1) * sizeof(char));
        copyAndTerminateString(returnHeaders[n], headers, len);
        headers = nextLine + 2;
    }
    len = strlen(headers); //here to returnHeaders[n+1] = NULL; is what I've added that's caused this error with some websites
    if (len == 0) return returnHeaders;
    returnHeaders = realloc(returnHeaders, (n + 1) * sizeof(char *));
    if (returnHeaders == NULL) {
        printf("Failure to allocate memory at line %d.\r\n", __LINE__ - 2);
        exit(1);
    }
    returnHeaders[n] = malloc((len + 1) * sizeof(char));
    copyAndTerminateString(returnHeaders[n], headers, len);
    returnHeaders[n+1] = NULL;
    return returnHeaders;
}

Here is how I free the structure containing the data in returnHeaders:

以下是我如何在returnHeaders中釋放包含數據的結構:

void freeHTTPresponse(HTTPresponse *freeResponse) {
    int n;
    for (n = 0; freeResponse->headers[n] != NULL; n++) {
        free(freeResponse->headers[n]);
    }
    free(freeResponse->headers);
    free(freeResponse->content);
    free(freeResponse);
}

Here is the full code (edited to only print out information pertaining to my problem): http://pastebin.com/AWWTLpWH

這是完整的代碼(編輯后只打印出與我的問題有關的信息):http://pastebin.com/AWWTLpWH

1 个解决方案

#1


This line:

returnHeaders[n+1] = NULL;

writes past the end of the returnHeaders array (which has n+1 elements).

寫入returnHeaders數組的末尾(具有n + 1個元素)。


注意!

本站翻译的文章,版权归属于本站,未经许可禁止转摘,转摘请注明本文地址:https://www.itdaan.com/blog/2015/05/04/ab224cdeaa186d8e0878209bb8158a57.html



 
粤ICP备14056181号  © 2014-2021 ITdaan.com