简体   繁体   中英

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

The libcurl examples contains an example for custom HTTP headers .

The example makes use of curl_slist_append like this:

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;");

According to the documentation of curl_slist_append a null pointer will be returned if something goes wrong:

RETURN VALUE

A null pointer is returned if anything went wrong, otherwise the new list pointer is returned.

Question:

When for example the call

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

fails, won't the original list, that chunk previously pointed to, be lost? And as a consequence: won't this leak memory? Or is there some magic that I am missing and that is not mentioned in the curl_slist_append documentation?

To make matters worse: won't the next call to curl_slist_append possibly create a new list (unlikely as we are probably out of memory already, but possible)?

Your suspicions seem entirely correct. The source for curl_slist_append can be viewed here .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM