c-realloc():下一个大小无效.无法将http页面读取为字符串



我正在尝试从HTTP页面读取并将整个页面保存为字符串。当realloc((处理内存时,出现错误,声明:realloc(:无效的下一个大小: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,因此复制的数据会转到错误的地址,从而导致堆损坏。

相关内容

  • 没有找到相关文章

最新更新