Skip to content

Commit 0927156

Browse files
committed
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.
1 parent db30108 commit 0927156

File tree

1 file changed

+14
-16
lines changed

1 file changed

+14
-16
lines changed

src/config_file.c

Lines changed: 14 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -64,21 +64,21 @@ static char *escape_value(const char *ptr);
6464
* refcount. This is its own function to make sure we use the mutex to
6565
* avoid the map pointer from changing under us.
6666
*/
67-
static git_config_entries *config_file_entries_take(config_file_backend *b)
67+
static int config_file_entries_take(git_config_entries **out, config_file_backend *b)
6868
{
69-
git_config_entries *entries;
69+
int error;
7070

71-
if (git_mutex_lock(&b->values_mutex) < 0) {
72-
git_error_set(GIT_ERROR_OS, "failed to lock config backend");
73-
return NULL;
71+
if ((error = git_mutex_lock(&b->values_mutex)) < 0) {
72+
git_error_set(GIT_ERROR_OS, "failed to lock config backend");
73+
return error;
7474
}
7575

76-
entries = b->entries;
77-
git_config_entries_incref(entries);
76+
git_config_entries_incref(b->entries);
77+
*out = b->entries;
7878

7979
git_mutex_unlock(&b->values_mutex);
8080

81-
return entries;
81+
return 0;
8282
}
8383

8484
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
280280
if ((error = git_config__normalize_name(name, &key)) < 0)
281281
return error;
282282

283-
if ((entries = config_file_entries_take(b)) == NULL)
284-
return -1;
283+
if ((error = config_file_entries_take(&entries, b)) < 0)
284+
return error;
285285

286286
/* Check whether we'd be modifying an included or multivar key */
287287
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_
331331
if (!h->parent.readonly && ((error = config_file_refresh(cfg)) < 0))
332332
return error;
333333

334-
if ((entries = config_file_entries_take(h)) == NULL)
335-
return -1;
334+
if ((error = config_file_entries_take(&entries, h)) < 0)
335+
return error;
336336

337337
if ((error = (git_config_entries_get(&entry, entries, key))) < 0) {
338338
git_config_entries_free(entries);
@@ -384,7 +384,7 @@ static int config_file_delete(git_config_backend *cfg, const char *name)
384384
if ((error = git_config__normalize_name(name, &key)) < 0)
385385
goto out;
386386

387-
if ((entries = config_file_entries_take(b)) == NULL)
387+
if ((error = config_file_entries_take(&entries, b)) < 0)
388388
goto out;
389389

390390
/* 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
415415
if ((result = git_config__normalize_name(name, &key)) < 0)
416416
goto out;
417417

418-
if ((entries = config_file_entries_take(b)) == NULL) {
419-
result = -1;
418+
if ((result = config_file_entries_take(&entries, b)) < 0)
420419
goto out;
421-
}
422420

423421
if ((result = git_config_entries_get(&entry, entries, key)) < 0) {
424422
if (result == GIT_ENOTFOUND)

0 commit comments

Comments
 (0)