http: refactor code to clarify memory ownership

There are various variables assigned via `git_config_string()` and `git_config_pathname()` which are never free'd. This bug is relatable because the out parameter of those functions are a `const char **`, even though memory ownership is transferred to the caller. We're about to adapt the functions to instead use `char **`. Prepare the code accordingly. Note that the `(const char **)` casts will go away once we have adapted the functions. Signed-off-by: Patrick Steinhardt <ps@pks.im> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Patrick Steinhardt committed May 27, 2024 at 13:46 UTC f962ffc392fe1831d047a10a28a55710d987d746
1 file changed +32 -30
http.c
+32 -30
@@ -39,8 +39,8 @@ char curl_errorstr[CURL_ERROR_SIZE];
39 static int curl_ssl_verify = -1;
40 static int curl_ssl_try;
41 static const char *curl_http_version = NULL;
42 -static const char *ssl_cert;
43 -static const char *ssl_cert_type;
42 +static char *ssl_cert;
43 +static char *ssl_cert_type;
44 static const char *ssl_cipherlist;
45 static const char *ssl_version;
46 static struct {
@@ -59,23 +59,23 @@ static struct {
59 { "tlsv1.3", CURL_SSLVERSION_TLSv1_3 },
60 #endif
61 };
62 -static const char *ssl_key;
63 -static const char *ssl_key_type;
64 -static const char *ssl_capath;
65 -static const char *curl_no_proxy;
62 +static char *ssl_key;
63 +static char *ssl_key_type;
64 +static char *ssl_capath;
65 +static char *curl_no_proxy;
66 #ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
67 static const char *ssl_pinnedkey;
68 #endif
69 -static const char *ssl_cainfo;
69 +static char *ssl_cainfo;
70 static long curl_low_speed_limit = -1;
71 static long curl_low_speed_time = -1;
72 static int curl_ftp_no_epsv;
73 -static const char *curl_http_proxy;
74 -static const char *http_proxy_authmethod;
73 +static char *curl_http_proxy;
74 +static char *http_proxy_authmethod;
75
76 -static const char *http_proxy_ssl_cert;
77 -static const char *http_proxy_ssl_key;
78 -static const char *http_proxy_ssl_ca_info;
76 +static char *http_proxy_ssl_cert;
77 +static char *http_proxy_ssl_key;
78 +static char *http_proxy_ssl_ca_info;
79 static struct credential proxy_cert_auth = CREDENTIAL_INIT;
80 static int proxy_ssl_cert_password_required;
81
@@ -112,7 +112,7 @@ static const char *curl_cookie_file;
112 static int curl_save_cookies;
113 struct credential http_auth = CREDENTIAL_INIT;
114 static int http_proactive_auth;
115 -static const char *user_agent;
115 +static char *user_agent;
116 static int curl_empty_auth = -1;
117
118 enum http_follow_config http_follow_config = HTTP_FOLLOW_INITIAL;
@@ -381,17 +381,17 @@ static int http_options(const char *var, const char *value,
381 if (!strcmp("http.sslversion", var))
382 return git_config_string(&ssl_version, var, value);
383 if (!strcmp("http.sslcert", var))
384 - return git_config_pathname(&ssl_cert, var, value);
384 + return git_config_pathname((const char **)&ssl_cert, var, value);
385 if (!strcmp("http.sslcerttype", var))
386 - return git_config_string(&ssl_cert_type, var, value);
386 + return git_config_string((const char **)&ssl_cert_type, var, value);
387 if (!strcmp("http.sslkey", var))
388 - return git_config_pathname(&ssl_key, var, value);
388 + return git_config_pathname((const char **)&ssl_key, var, value);
389 if (!strcmp("http.sslkeytype", var))
390 - return git_config_string(&ssl_key_type, var, value);
390 + return git_config_string((const char **)&ssl_key_type, var, value);
391 if (!strcmp("http.sslcapath", var))
392 - return git_config_pathname(&ssl_capath, var, value);
392 + return git_config_pathname((const char **)&ssl_capath, var, value);
393 if (!strcmp("http.sslcainfo", var))
394 - return git_config_pathname(&ssl_cainfo, var, value);
394 + return git_config_pathname((const char **)&ssl_cainfo, var, value);
395 if (!strcmp("http.sslcertpasswordprotected", var)) {
396 ssl_cert_password_required = git_config_bool(var, value);
397 return 0;
@@ -440,19 +440,19 @@ static int http_options(const char *var, const char *value,
440 return 0;
441 }
442 if (!strcmp("http.proxy", var))
443 - return git_config_string(&curl_http_proxy, var, value);
443 + return git_config_string((const char **)&curl_http_proxy, var, value);
444
445 if (!strcmp("http.proxyauthmethod", var))
446 - return git_config_string(&http_proxy_authmethod, var, value);
446 + return git_config_string((const char **)&http_proxy_authmethod, var, value);
447
448 if (!strcmp("http.proxysslcert", var))
449 - return git_config_string(&http_proxy_ssl_cert, var, value);
449 + return git_config_string((const char **)&http_proxy_ssl_cert, var, value);
450
451 if (!strcmp("http.proxysslkey", var))
452 - return git_config_string(&http_proxy_ssl_key, var, value);
452 + return git_config_string((const char **)&http_proxy_ssl_key, var, value);
453
454 if (!strcmp("http.proxysslcainfo", var))
455 - return git_config_string(&http_proxy_ssl_ca_info, var, value);
455 + return git_config_string((const char **)&http_proxy_ssl_ca_info, var, value);
456
457 if (!strcmp("http.proxysslcertpasswordprotected", var)) {
458 proxy_ssl_cert_password_required = git_config_bool(var, value);
@@ -476,7 +476,7 @@ static int http_options(const char *var, const char *value,
476 }
477
478 if (!strcmp("http.useragent", var))
479 - return git_config_string(&user_agent, var, value);
479 + return git_config_string((const char **)&user_agent, var, value);
480
481 if (!strcmp("http.emptyauth", var)) {
482 if (value && !strcmp("auto", value))
@@ -592,10 +592,10 @@ static void init_curl_http_auth(CURL *result)
592 }
593
594 /* *var must be free-able */
595 -static void var_override(const char **var, char *value)
595 +static void var_override(char **var, char *value)
596 {
597 if (value) {
598 - free((void *)*var);
598 + free(*var);
599 *var = xstrdup(value);
600 }
601 }
@@ -1233,11 +1233,13 @@ static CURL *get_curl_handle(void)
1233 return result;
1234 }
1235
1236 -static void set_from_env(const char **var, const char *envname)
1236 +static void set_from_env(char **var, const char *envname)
1237 {
1238 const char *val = getenv(envname);
1239 - if (val)
1240 - *var = val;
1239 + if (val) {
1240 + FREE_AND_NULL(*var);
1241 + *var = xstrdup(val);
1242 + }
1243 }
1244
1245 void http_init(struct remote *remote, const char *url, int proactive_auth)