下面是一段基于简单结构在套接字上编写http响应的代码
void write_response(request *req, response *resp, int socket) {
char *raw_resp;
int bytes = 0;
asprintf(&raw_resp, "HTTP/1.1 %d %s\r\n", resp->code, resp->reason_phrase);
bytes += strlen(raw_resp);
for (int i = 0; i < resp->header_count; i++) {
asprintf(&raw_resp, "%s%s", raw_resp, resp->headers[i]);
bytes += strlen(resp->headers[i]);
}
if (resp->content != NULL) {
asprintf(&raw_resp, "%s\r\n", raw_resp);
raw_resp = realloc(raw_resp, bytes + 2 + resp->content->size);
memcpy(&raw_resp[strlen(raw_resp)], resp->content->data,
resp->content->size);
bytes += (resp->content->size + 2);
}
write(socket, raw_resp, bytes);
free(raw_resp);
}
基本上,它首先添加http请求行,然后添加头,最后添加主体(如果需要)。
然而,valgrind在前两个asprintf上报告了
Invalid free() / delete / delete[] / realloc()
和18 bytes in 1 blocks are definitely lost in loss record 2 of 4
(内存泄漏),但奇怪的是在第三个asprintf上没有。我是不是在用asprintf?
最佳答案
您的代码没有free
分配的所有字符串。考虑到如何使用cc执行动态字符串连接,解决这个问题有点麻烦。请注意,您也不处理内存分配失败。您可以使用asprintf
返回值来检测并更新asprintf
,而不需要额外的asprintf
调用。
/* return the number of bytes written or -1 in case of error */
int write_response(request *req, response *resp, int socket) {
char *raw_resp, *new_p;
int bytes;
bytes = asprintf(&raw_resp, "HTTP/1.1 %d %s\r\n", resp->code, resp->reason_phrase);
if (bytes < 0)
return -1;
for (int i = 0; i < resp->header_count; i++) {
bytes = asprintf(&new_p, "%s%s", raw_resp, resp->headers[i]);
free(raw_resp);
raw_resp = newp;
if (bytes < 0)
return -1;
}
if (resp->content != NULL) {
bytes = asprintf(&new_p, "%s\r\n", raw_resp);
free(raw_resp);
raw_resp = newp;
if (bytes < 0)
return -1;
new_p = realloc(raw_resp, bytes + resp->content->size);
if (new_p == NULL) {
free(raw_resp);
return -1;
}
raw_resp = new_p;
memcpy(raw_resp + bytes, resp->content->data, resp->content->size);
bytes += resp->content->size;
}
bytes = write(socket, raw_resp, bytes);
free(raw_resp);
return bytes;
}
评论:
使用
bytes
执行带分配的字符串连接似乎效率低下,只需使用strlen()
、asprintf
和strlen
。realloc
是非标准的,并非所有平台都可用。除非您被要求发出一个对
memcpy
的调用,否则单独编写内容可能会更有效,以避免对asprintf()
的额外调用,从而可能占用大量内存。在初始阶段使用
write
和realloc()
计算报头的长度并将报头的空间直接分配到最大值,或者使用本地数组在低于合理阈值(4k)时甚至不分配,这可能更有效。以下是修改版本:
int write_response(request *req, response *resp, int socket) {
char buffer[4096];
char *raw_resp, *allocated = NULL;
int bytes, pos;
bytes = snprintf(NULL, 0, "HTTP/1.1 %d %s\r\n", resp->code, resp->reason_phrase);
for (int i = 0; i < resp->header_count; i++)
bytes += strlen(resp->headers[i]);
if (resp->content != NULL)
bytes += 2 + resp->content->size;
/* need an extra byte for `snprintf` null terminator
if no headers and no contents */
if (bytes < sizeof(buffer)) {
raw_resp = buffer;
} else {
raw_resp = allocated = malloc(bytes + 1):
if (raw_resp == NULL)
return -1;
}
pos = snprintf(raw_resp, bytes, "HTTP/1.1 %d %s\r\n", resp->code, resp->reason_phrase);
for (int i = 0; i < resp->header_count; i++) {
int len = strlen(resp->headers[i]);
memcpy(raw_resp + pos, resp->headers[i], len);
pos += len;
}
if (resp->content != NULL) {
raw_resp[pos++] = '\r';
raw_resp[pos++] = '\n';
memcpy(raw_resp + pos, resp->content->data, resp->content->size);
pos += resp->content->size;
}
bytes = write(socket, raw_resp, bytes);
free(allocated);
return bytes;
}
关于c - Valgrind使用asprintf报告内存泄漏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/54781388/