Skip to content

Commit

Permalink
squash! remote-curl: unbreak http.extraHeader with custom allocators
Browse files Browse the repository at this point in the history
remote-curl: unbreak http.extraHeader with custom allocators

In 93b980e (http: use xmalloc with cURL, 2019-08-15), we started to
ask cURL to use `xmalloc()`, and if compiled with nedmalloc, that means
implicitly a different allocator than the system one.

Which means that all of cURL's allocations and releases now _need_ to
use that allocator.

However, the `http_options()` function used `slist_append()` to add any
configured extra HTTP header(s) _before_ asking cURL to use `xmalloc()`,
and `http_cleanup()` would release them _afterwards_, i.e. in the
presence of custom allocators, cURL would attempt to use the wrong
allocator to release the memory.

A naïve attempt at fixing this would move the call to
`curl_global_init()` _before_ the config is parsed (i.e. before that
call to `slist_append()`).

However, that does work, as we _also_ parse the config setting
`http.sslbackend` and if found, call `curl_global_sslset()` which *must*
be called before `curl_global_init()`, for details see:
https://curl.haxx.se/libcurl/c/curl_global_sslset.html

So let's instead make the config parsing entirely independent from
cURL's data structures. Incidentally, this deletes two more lines than
it introduces, which is nice.

Signed-off-by: Johannes Schindelin <johannes.schindelin@gmx.de>
  • Loading branch information
dscho committed Nov 6, 2019
1 parent ffb6f1c commit 02af2cc
Showing 1 changed file with 11 additions and 13 deletions.
24 changes: 11 additions & 13 deletions http.c
Original file line number Diff line number Diff line change
Expand Up @@ -150,7 +150,7 @@ static unsigned long empty_auth_useless =

static struct curl_slist *pragma_header;
static struct curl_slist *no_pragma_header;
static struct curl_slist *extra_http_headers;
static struct string_list extra_http_headers = STRING_LIST_INIT_DUP;

static struct active_request_slot *active_queue_head;

Expand Down Expand Up @@ -414,11 +414,9 @@ static int http_options(const char *var, const char *value, void *cb)
if (!value) {
return config_error_nonbool(var);
} else if (!*value) {
curl_slist_free_all(extra_http_headers);
extra_http_headers = NULL;
string_list_clear(&extra_http_headers, 0);
} else {
extra_http_headers =
curl_slist_append(extra_http_headers, value);
string_list_append(&extra_http_headers, value);
}
return 0;
}
Expand Down Expand Up @@ -1064,9 +1062,6 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
char *normalized_url;
struct urlmatch_config config = { STRING_LIST_INIT_DUP };

if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
die("curl_global_init failed");

config.section = "http";
config.key = NULL;
config.collect_fn = http_options;
Expand Down Expand Up @@ -1107,6 +1102,9 @@ void http_init(struct remote *remote, const char *url, int proactive_auth)
}
#endif

if (curl_global_init(CURL_GLOBAL_ALL) != CURLE_OK)
die("curl_global_init failed");

http_proactive_auth = proactive_auth;

if (remote && remote->http_proxy)
Expand Down Expand Up @@ -1202,8 +1200,7 @@ void http_cleanup(void)
#endif
curl_global_cleanup();

curl_slist_free_all(extra_http_headers);
extra_http_headers = NULL;
string_list_clear(&extra_http_headers, 0);

curl_slist_free_all(pragma_header);
pragma_header = NULL;
Expand Down Expand Up @@ -1627,10 +1624,11 @@ int run_one_slot(struct active_request_slot *slot,

struct curl_slist *http_copy_default_headers(void)
{
struct curl_slist *headers = NULL, *h;
struct curl_slist *headers = NULL;
const struct string_list_item *item;

for (h = extra_http_headers; h; h = h->next)
headers = curl_slist_append(headers, h->data);
for_each_string_list_item(item, &extra_http_headers)
headers = curl_slist_append(headers, item->string);

return headers;
}
Expand Down

0 comments on commit 02af2cc

Please sign in to comment.