Commit 0927156a by Patrick Steinhardt

config_file: refactor taking entries ref to return an error code

The function to take a reference to the config file's config entries
currently returns the reference via return value. Due to this, it's
harder than necessary to integrate into our typical coding style, as one
needs to make sure that a proper error code is set before erroring out
from the caller. This bites us in `config_file_delete`, where we call
`goto out` directly when `config_file_entries_take` returns `NULL`, but
we actually forget to set up the error code and thus return success.

Fix the issue by refactoring the function to return an error code and
pass the reference via an out-pointer.
parent db301087
...@@ -64,21 +64,21 @@ static char *escape_value(const char *ptr); ...@@ -64,21 +64,21 @@ static char *escape_value(const char *ptr);
* refcount. This is its own function to make sure we use the mutex to * refcount. This is its own function to make sure we use the mutex to
* avoid the map pointer from changing under us. * avoid the map pointer from changing under us.
*/ */
static git_config_entries *config_file_entries_take(config_file_backend *b) static int config_file_entries_take(git_config_entries **out, config_file_backend *b)
{ {
git_config_entries *entries; int error;
if (git_mutex_lock(&b->values_mutex) < 0) { if ((error = git_mutex_lock(&b->values_mutex)) < 0) {
git_error_set(GIT_ERROR_OS, "failed to lock config backend"); git_error_set(GIT_ERROR_OS, "failed to lock config backend");
return NULL; return error;
} }
entries = b->entries; git_config_entries_incref(b->entries);
git_config_entries_incref(entries); *out = b->entries;
git_mutex_unlock(&b->values_mutex); git_mutex_unlock(&b->values_mutex);
return entries; return 0;
} }
static void config_file_clear(config_file *file) static void config_file_clear(config_file *file)
...@@ -280,8 +280,8 @@ static int config_file_set(git_config_backend *cfg, const char *name, const char ...@@ -280,8 +280,8 @@ static int config_file_set(git_config_backend *cfg, const char *name, const char
if ((error = git_config__normalize_name(name, &key)) < 0) if ((error = git_config__normalize_name(name, &key)) < 0)
return error; return error;
if ((entries = config_file_entries_take(b)) == NULL) if ((error = config_file_entries_take(&entries, b)) < 0)
return -1; return error;
/* Check whether we'd be modifying an included or multivar key */ /* Check whether we'd be modifying an included or multivar key */
if ((error = git_config_entries_get_unique(&existing, entries, key)) < 0) { if ((error = git_config_entries_get_unique(&existing, entries, key)) < 0) {
...@@ -331,8 +331,8 @@ static int config_file_get(git_config_backend *cfg, const char *key, git_config_ ...@@ -331,8 +331,8 @@ static int config_file_get(git_config_backend *cfg, const char *key, git_config_
if (!h->parent.readonly && ((error = config_file_refresh(cfg)) < 0)) if (!h->parent.readonly && ((error = config_file_refresh(cfg)) < 0))
return error; return error;
if ((entries = config_file_entries_take(h)) == NULL) if ((error = config_file_entries_take(&entries, h)) < 0)
return -1; return error;
if ((error = (git_config_entries_get(&entry, entries, key))) < 0) { if ((error = (git_config_entries_get(&entry, entries, key))) < 0) {
git_config_entries_free(entries); git_config_entries_free(entries);
...@@ -384,7 +384,7 @@ static int config_file_delete(git_config_backend *cfg, const char *name) ...@@ -384,7 +384,7 @@ static int config_file_delete(git_config_backend *cfg, const char *name)
if ((error = git_config__normalize_name(name, &key)) < 0) if ((error = git_config__normalize_name(name, &key)) < 0)
goto out; goto out;
if ((entries = config_file_entries_take(b)) == NULL) if ((error = config_file_entries_take(&entries, b)) < 0)
goto out; goto out;
/* Check whether we'd be modifying an included or multivar key */ /* Check whether we'd be modifying an included or multivar key */
...@@ -415,10 +415,8 @@ static int config_file_delete_multivar(git_config_backend *cfg, const char *name ...@@ -415,10 +415,8 @@ static int config_file_delete_multivar(git_config_backend *cfg, const char *name
if ((result = git_config__normalize_name(name, &key)) < 0) if ((result = git_config__normalize_name(name, &key)) < 0)
goto out; goto out;
if ((entries = config_file_entries_take(b)) == NULL) { if ((result = config_file_entries_take(&entries, b)) < 0)
result = -1;
goto out; goto out;
}
if ((result = git_config_entries_get(&entry, entries, key)) < 0) { if ((result = git_config_entries_get(&entry, entries, key)) < 0) {
if (result == GIT_ENOTFOUND) if (result == GIT_ENOTFOUND)
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment