libcurl示例httpcustomheader.c有bug(显示不良实践)还是我错过了一些东西

Is the libcurl example httpcustomheader.c buggy (showing bad practice) or am I missing something?

本文关键字:错过了 httpcustomheader 示例 bug 不良 显示 libcurl      更新时间:2023-10-16

libcurl示例包含一个自定义HTTP头的示例。

这个例子像这样使用curl_slist_append:

struct curl_slist *chunk = NULL;
/* Remove a header curl would otherwise add by itself */ 
chunk = curl_slist_append(chunk, "Accept:");
/* Add a custom header */ 
chunk = curl_slist_append(chunk, "Another: yes");
/* Modify a header curl otherwise adds differently */ 
chunk = curl_slist_append(chunk, "Host: example.com");
/* Add a header with "blank" contents to the right of the colon. Note that
   we're then using a semicolon in the string we pass to curl! */ 
chunk = curl_slist_append(chunk, "X-silly-header;");

根据curl_slist_append的文档,如果出现错误,将返回空指针:

返回值

如果出现任何错误,则返回空指针,否则返回new返回列表指针

问题:

例如调用

chunk = curl_slist_append(chunk, "Another: yes");

失败了,原来的列表,之前指向的块,不会丢失吗?结果是:这不会泄漏内存吗?还是我缺少了一些魔法,而在curl_slist_append文档中没有提到?

更糟糕的是:下次调用curl_slist_append可能不会创建一个新的列表(不太可能,因为我们可能已经没有内存了,但可能)?

你的怀疑似乎完全正确。curl_slist_append的源代码可以在这里查看。