realloc():下一个大小无效。无法将 http 页面读入字符串

realloc(): invalid next size. Fail to read the http page into string

我正在尝试从 HTTP 页面读取并将整个页面保存为字符串。 realloc() 内存时,出现错误,指出: realloc(): invalid next size: 0x0000000001505e10 ***...

下面是我的代码,在此先感谢

char* get_http(int my_socket, char* buffer){
    char* http_page = calloc(BUFFER_SIZE, sizeof(char));
    assert(http_page);
    int cur_size = BUFFER_SIZE;
    int cur_len = 0;

    // read the http, and write into http_page

    bzero(buffer, BUFFER_SIZE);
    while (read(my_socket, buffer, BUFFER_SIZE - 1) != 0){
        // if the size if not enough, reallocate the memory
        if (cur_len + strlen(buffer) >= cur_size){
            cur_size = cur_size* 2;
            http_page = (char*)realloc(http_page, cur_size);
            assert(http_page);
        }
        cur_len += strlen(buffer);
        strcpy(http_page + cur_len, buffer);
        bzero(buffer, BUFFER_SIZE);
    }
    return http_page;}

问题在这里:

cur_len += strlen(buffer);
strcpy(http_page + cur_len, buffer);

您在复制之前更新了 cur_len,因此复制的数据进入了错误的地址,导致堆损坏。