Merge branch 'ps/leakfixes' into ps/leakfixes-more

* ps/leakfixes:
  builtin/mv: fix leaks for submodule gitfile paths
  builtin/mv: refactor to use `struct strvec`
  builtin/mv duplicate string list memory
  builtin/mv: refactor `add_slash()` to always return allocated strings
  strvec: add functions to replace and remove strings
  submodule: fix leaking memory for submodule entries
  commit-reach: fix memory leak in `ahead_behind()`
  builtin/credential: clear credential before exit
  config: plug various memory leaks
  config: clarify memory ownership in `git_config_string()`
  builtin/log: stop using globals for format config
  builtin/log: stop using globals for log config
  convert: refactor code to clarify ownership of check_roundtrip_encoding
  diff: refactor code to clarify memory ownership of prefixes
  config: clarify memory ownership in `git_config_pathname()`
  http: refactor code to clarify memory ownership
  checkout: clarify memory ownership in `unique_tracking_name()`
  strbuf: fix leak when `appendwholeline()` fails with EOF
  transport-helper: fix leaking helper name
This commit is contained in:
Junio C Hamano
2024-06-03 13:08:33 -07:00
97 changed files with 1141 additions and 588 deletions

View File

@ -1338,7 +1338,7 @@ int git_config_bool(const char *name, const char *value)
return v;
}
int git_config_string(const char **dest, const char *var, const char *value)
int git_config_string(char **dest, const char *var, const char *value)
{
if (!value)
return config_error_nonbool(var);
@ -1346,7 +1346,7 @@ int git_config_string(const char **dest, const char *var, const char *value)
return 0;
}
int git_config_pathname(const char **dest, const char *var, const char *value)
int git_config_pathname(char **dest, const char *var, const char *value)
{
if (!value)
return config_error_nonbool(var);
@ -1414,11 +1414,15 @@ static int git_default_core_config(const char *var, const char *value,
return 0;
}
if (!strcmp(var, "core.attributesfile"))
if (!strcmp(var, "core.attributesfile")) {
FREE_AND_NULL(git_attributes_file);
return git_config_pathname(&git_attributes_file, var, value);
}
if (!strcmp(var, "core.hookspath"))
if (!strcmp(var, "core.hookspath")) {
FREE_AND_NULL(git_hooks_path);
return git_config_pathname(&git_hooks_path, var, value);
}
if (!strcmp(var, "core.bare")) {
is_bare_repository_cfg = git_config_bool(var, value);
@ -1553,8 +1557,10 @@ static int git_default_core_config(const char *var, const char *value,
return 0;
}
if (!strcmp(var, "core.checkroundtripencoding"))
if (!strcmp(var, "core.checkroundtripencoding")) {
FREE_AND_NULL(check_roundtrip_encoding);
return git_config_string(&check_roundtrip_encoding, var, value);
}
if (!strcmp(var, "core.notesref")) {
if (!value)
@ -1563,8 +1569,10 @@ static int git_default_core_config(const char *var, const char *value,
return 0;
}
if (!strcmp(var, "core.editor"))
if (!strcmp(var, "core.editor")) {
FREE_AND_NULL(editor_program);
return git_config_string(&editor_program, var, value);
}
if (!strcmp(var, "core.commentchar") ||
!strcmp(var, "core.commentstring")) {
@ -1582,11 +1590,13 @@ static int git_default_core_config(const char *var, const char *value,
return 0;
}
if (!strcmp(var, "core.askpass"))
if (!strcmp(var, "core.askpass")) {
FREE_AND_NULL(askpass_program);
return git_config_string(&askpass_program, var, value);
}
if (!strcmp(var, "core.excludesfile")) {
free((char *)excludes_file);
FREE_AND_NULL(excludes_file);
return git_config_pathname(&excludes_file, var, value);
}
@ -1689,11 +1699,15 @@ static int git_default_sparse_config(const char *var, const char *value)
static int git_default_i18n_config(const char *var, const char *value)
{
if (!strcmp(var, "i18n.commitencoding"))
if (!strcmp(var, "i18n.commitencoding")) {
FREE_AND_NULL(git_commit_encoding);
return git_config_string(&git_commit_encoding, var, value);
}
if (!strcmp(var, "i18n.logoutputencoding"))
if (!strcmp(var, "i18n.logoutputencoding")) {
FREE_AND_NULL(git_log_output_encoding);
return git_config_string(&git_log_output_encoding, var, value);
}
/* Add other config variables here and to Documentation/config.txt. */
return 0;
@ -1766,10 +1780,15 @@ static int git_default_push_config(const char *var, const char *value)
static int git_default_mailmap_config(const char *var, const char *value)
{
if (!strcmp(var, "mailmap.file"))
if (!strcmp(var, "mailmap.file")) {
FREE_AND_NULL(git_mailmap_file);
return git_config_pathname(&git_mailmap_file, var, value);
if (!strcmp(var, "mailmap.blob"))
}
if (!strcmp(var, "mailmap.blob")) {
FREE_AND_NULL(git_mailmap_blob);
return git_config_string(&git_mailmap_blob, var, value);
}
/* Add other config variables here and to Documentation/config.txt. */
return 0;
@ -1777,8 +1796,10 @@ static int git_default_mailmap_config(const char *var, const char *value)
static int git_default_attr_config(const char *var, const char *value)
{
if (!strcmp(var, "attr.tree"))
if (!strcmp(var, "attr.tree")) {
FREE_AND_NULL(git_attr_tree);
return git_config_string(&git_attr_tree, var, value);
}
/*
* Add other attribute related config variables here and to
@ -2405,7 +2426,7 @@ int git_configset_get_string(struct config_set *set, const char *key, char **des
{
const char *value;
if (!git_configset_get_value(set, key, &value, NULL))
return git_config_string((const char **)dest, key, value);
return git_config_string(dest, key, value);
else
return 1;
}
@ -2483,7 +2504,7 @@ int git_configset_get_maybe_bool(struct config_set *set, const char *key, int *d
return 1;
}
int git_configset_get_pathname(struct config_set *set, const char *key, const char **dest)
int git_configset_get_pathname(struct config_set *set, const char *key, char **dest)
{
const char *value;
if (!git_configset_get_value(set, key, &value, NULL))
@ -2628,7 +2649,7 @@ int repo_config_get_maybe_bool(struct repository *repo,
}
int repo_config_get_pathname(struct repository *repo,
const char *key, const char **dest)
const char *key, char **dest)
{
int ret;
git_config_check_init(repo);
@ -2727,7 +2748,7 @@ int git_config_get_maybe_bool(const char *key, int *dest)
return repo_config_get_maybe_bool(the_repository, key, dest);
}
int git_config_get_pathname(const char *key, const char **dest)
int git_config_get_pathname(const char *key, char **dest)
{
return repo_config_get_pathname(the_repository, key, dest);
}