Skip to content

Commit f962ffc

Browse files
pks-tgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent cc395d6 commit f962ffc

File tree

1 file changed

+32
-30
lines changed

1 file changed

+32
-30
lines changed

http.c

Lines changed: 32 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -39,8 +39,8 @@ char curl_errorstr[CURL_ERROR_SIZE];
3939
static int curl_ssl_verify = -1;
4040
static int curl_ssl_try;
4141
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;
4444
static const char *ssl_cipherlist;
4545
static const char *ssl_version;
4646
static struct {
@@ -59,23 +59,23 @@ static struct {
5959
{ "tlsv1.3", CURL_SSLVERSION_TLSv1_3 },
6060
#endif
6161
};
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;
6666
#ifdef GIT_CURL_HAVE_CURLOPT_PINNEDPUBLICKEY
6767
static const char *ssl_pinnedkey;
6868
#endif
69-
static const char *ssl_cainfo;
69+
static char *ssl_cainfo;
7070
static long curl_low_speed_limit = -1;
7171
static long curl_low_speed_time = -1;
7272
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;
7575

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;
7979
static struct credential proxy_cert_auth = CREDENTIAL_INIT;
8080
static int proxy_ssl_cert_password_required;
8181

@@ -112,7 +112,7 @@ static const char *curl_cookie_file;
112112
static int curl_save_cookies;
113113
struct credential http_auth = CREDENTIAL_INIT;
114114
static int http_proactive_auth;
115-
static const char *user_agent;
115+
static char *user_agent;
116116
static int curl_empty_auth = -1;
117117

118118
enum http_follow_config http_follow_config = HTTP_FOLLOW_INITIAL;
@@ -381,17 +381,17 @@ static int http_options(const char *var, const char *value,
381381
if (!strcmp("http.sslversion", var))
382382
return git_config_string(&ssl_version, var, value);
383383
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);
385385
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);
387387
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);
389389
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);
391391
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);
393393
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);
395395
if (!strcmp("http.sslcertpasswordprotected", var)) {
396396
ssl_cert_password_required = git_config_bool(var, value);
397397
return 0;
@@ -440,19 +440,19 @@ static int http_options(const char *var, const char *value,
440440
return 0;
441441
}
442442
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);
444444

445445
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);
447447

448448
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);
450450

451451
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);
453453

454454
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);
456456

457457
if (!strcmp("http.proxysslcertpasswordprotected", var)) {
458458
proxy_ssl_cert_password_required = git_config_bool(var, value);
@@ -476,7 +476,7 @@ static int http_options(const char *var, const char *value,
476476
}
477477

478478
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);
480480

481481
if (!strcmp("http.emptyauth", var)) {
482482
if (value && !strcmp("auto", value))
@@ -592,10 +592,10 @@ static void init_curl_http_auth(CURL *result)
592592
}
593593

594594
/* *var must be free-able */
595-
static void var_override(const char **var, char *value)
595+
static void var_override(char **var, char *value)
596596
{
597597
if (value) {
598-
free((void *)*var);
598+
free(*var);
599599
*var = xstrdup(value);
600600
}
601601
}
@@ -1233,11 +1233,13 @@ static CURL *get_curl_handle(void)
12331233
return result;
12341234
}
12351235

1236-
static void set_from_env(const char **var, const char *envname)
1236+
static void set_from_env(char **var, const char *envname)
12371237
{
12381238
const char *val = getenv(envname);
1239-
if (val)
1240-
*var = val;
1239+
if (val) {
1240+
FREE_AND_NULL(*var);
1241+
*var = xstrdup(val);
1242+
}
12411243
}
12421244

12431245
void http_init(struct remote *remote, const char *url, int proactive_auth)

0 commit comments

Comments
 (0)