Skip to content

Commit 1b261c2

Browse files
pks-tgitster
authored andcommitted
config: clarify memory ownership in git_config_string()
The out parameter of `git_config_string()` is a `const char **` even though we transfer ownership of memory to the caller. This is quite misleading and has led to many memory leaks all over the place. Adapt the parameter to instead be `char **`. Signed-off-by: Patrick Steinhardt <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 83024d9 commit 1b261c2

30 files changed

+96
-92
lines changed

alias.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ static int config_alias_cb(const char *key, const char *value,
2222

2323
if (data->alias) {
2424
if (!strcasecmp(p, data->alias))
25-
return git_config_string((const char **)&data->v,
25+
return git_config_string(&data->v,
2626
key, value);
2727
} else if (data->list) {
2828
string_list_append(data->list, p);

attr.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
#include "tree-walk.h"
2626
#include "object-name.h"
2727

28-
const char *git_attr_tree;
28+
char *git_attr_tree;
2929

3030
const char git_attr__true[] = "(builtin)true";
3131
const char git_attr__false[] = "\0(builtin)false";

attr.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,6 @@ const char *git_attr_global_file(void);
236236
/* Return whether the system gitattributes file is enabled and should be used. */
237237
int git_attr_system_is_enabled(void);
238238

239-
extern const char *git_attr_tree;
239+
extern char *git_attr_tree;
240240

241241
#endif /* ATTR_H */

builtin/commit.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ static struct strvec trailer_args = STRVEC_INIT;
133133
* is specified explicitly.
134134
*/
135135
static enum commit_msg_cleanup_mode cleanup_mode;
136-
static const char *cleanup_arg;
136+
static char *cleanup_arg;
137137

138138
static enum commit_whence whence;
139139
static int use_editor = 1, include_status = 1;

builtin/log.c

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -582,11 +582,11 @@ static int git_log_config(const char *var, const char *value,
582582

583583
if (!strcmp(var, "format.pretty")) {
584584
FREE_AND_NULL(cfg->fmt_pretty);
585-
return git_config_string((const char **) &cfg->fmt_pretty, var, value);
585+
return git_config_string(&cfg->fmt_pretty, var, value);
586586
}
587587
if (!strcmp(var, "format.subjectprefix")) {
588588
FREE_AND_NULL(cfg->fmt_patch_subject_prefix);
589-
return git_config_string((const char **) &cfg->fmt_patch_subject_prefix, var, value);
589+
return git_config_string(&cfg->fmt_patch_subject_prefix, var, value);
590590
}
591591
if (!strcmp(var, "format.filenamemaxlength")) {
592592
cfg->fmt_patch_name_max = git_config_int(var, value, ctx->kvi);
@@ -602,7 +602,7 @@ static int git_log_config(const char *var, const char *value,
602602
}
603603
if (!strcmp(var, "log.date")) {
604604
FREE_AND_NULL(cfg->default_date_mode);
605-
return git_config_string((const char **) &cfg->default_date_mode, var, value);
605+
return git_config_string(&cfg->default_date_mode, var, value);
606606
}
607607
if (!strcmp(var, "log.decorate")) {
608608
cfg->decoration_style = parse_decoration_style(value);
@@ -1076,7 +1076,7 @@ static int git_format_config(const char *var, const char *value,
10761076
}
10771077
if (!strcmp(var, "format.suffix")) {
10781078
FREE_AND_NULL(cfg->fmt_patch_suffix);
1079-
return git_config_string((const char **) &cfg->fmt_patch_suffix, var, value);
1079+
return git_config_string(&cfg->fmt_patch_suffix, var, value);
10801080
}
10811081
if (!strcmp(var, "format.to")) {
10821082
if (!value)
@@ -1133,7 +1133,7 @@ static int git_format_config(const char *var, const char *value,
11331133
}
11341134
if (!strcmp(var, "format.signature")) {
11351135
FREE_AND_NULL(cfg->signature);
1136-
return git_config_string((const char **) &cfg->signature, var, value);
1136+
return git_config_string(&cfg->signature, var, value);
11371137
}
11381138
if (!strcmp(var, "format.signaturefile")) {
11391139
FREE_AND_NULL(cfg->signature_file);
@@ -1149,7 +1149,7 @@ static int git_format_config(const char *var, const char *value,
11491149
}
11501150
if (!strcmp(var, "format.outputdirectory")) {
11511151
FREE_AND_NULL(cfg->config_output_directory);
1152-
return git_config_string((const char **) &cfg->config_output_directory, var, value);
1152+
return git_config_string(&cfg->config_output_directory, var, value);
11531153
}
11541154
if (!strcmp(var, "format.useautobase")) {
11551155
if (value && !strcasecmp(value, "whenAble")) {

builtin/merge.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,7 +100,7 @@ static struct strategy all_strategy[] = {
100100
{ "subtree", NO_FAST_FORWARD | NO_TRIVIAL },
101101
};
102102

103-
static const char *pull_twohead, *pull_octopus;
103+
static char *pull_twohead, *pull_octopus;
104104

105105
enum ff_type {
106106
FF_NO,
@@ -110,7 +110,7 @@ enum ff_type {
110110

111111
static enum ff_type fast_forward = FF_ALLOW;
112112

113-
static const char *cleanup_arg;
113+
static char *cleanup_arg;
114114
static enum commit_msg_cleanup_mode cleanup_mode;
115115

116116
static int option_parse_message(const struct option *opt,

builtin/rebase.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ static const char *action_names[] = {
8383
struct rebase_options {
8484
enum rebase_type type;
8585
enum empty_type empty;
86-
const char *default_backend;
86+
char *default_backend;
8787
const char *state_dir;
8888
struct commit *upstream;
8989
const char *upstream_name;

builtin/receive-pack.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ static struct strbuf push_cert = STRBUF_INIT;
8888
static struct object_id push_cert_oid;
8989
static struct signature_check sigcheck;
9090
static const char *push_cert_nonce;
91-
static const char *cert_nonce_seed;
91+
static char *cert_nonce_seed;
9292
static struct strvec hidden_refs = STRVEC_INIT;
9393

9494
static const char *NONCE_UNSOLICITED = "UNSOLICITED";

builtin/repack.c

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ static const char incremental_bitmap_conflict_error[] = N_(
4848
);
4949

5050
struct pack_objects_args {
51-
const char *window;
52-
const char *window_memory;
53-
const char *depth;
54-
const char *threads;
51+
char *window;
52+
char *window_memory;
53+
char *depth;
54+
char *threads;
5555
unsigned long max_pack_size;
5656
int no_reuse_delta;
5757
int no_reuse_object;

config.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1338,7 +1338,7 @@ int git_config_bool(const char *name, const char *value)
13381338
return v;
13391339
}
13401340

1341-
int git_config_string(const char **dest, const char *var, const char *value)
1341+
int git_config_string(char **dest, const char *var, const char *value)
13421342
{
13431343
if (!value)
13441344
return config_error_nonbool(var);
@@ -1566,7 +1566,7 @@ static int git_default_core_config(const char *var, const char *value,
15661566

15671567
if (!strcmp(var, "core.checkroundtripencoding")) {
15681568
FREE_AND_NULL(check_roundtrip_encoding);
1569-
return git_config_string((const char **) &check_roundtrip_encoding, var, value);
1569+
return git_config_string(&check_roundtrip_encoding, var, value);
15701570
}
15711571

15721572
if (!strcmp(var, "core.notesref")) {
@@ -2418,7 +2418,7 @@ int git_configset_get_string(struct config_set *set, const char *key, char **des
24182418
{
24192419
const char *value;
24202420
if (!git_configset_get_value(set, key, &value, NULL))
2421-
return git_config_string((const char **)dest, key, value);
2421+
return git_config_string(dest, key, value);
24222422
else
24232423
return 1;
24242424
}

0 commit comments

Comments
 (0)