config: don't use freed memory on error Change the order and set a NULL so we don't try to access freed memory in case of an error.
diff --git a/src/config_file.c b/src/config_file.c
index 746d965..4ccec2b 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -258,18 +258,17 @@ static int config_set(git_config_file *cfg, const char *name, const char *value)
GITERR_CHECK_ALLOC(var->value);
}
+ if (config_write(b, key, NULL, value) < 0) {
+ cvar_free(var);
+ return -1;
+ }
+
git_strmap_insert2(b->values, key, var, old_var, rval);
if (rval < 0)
return -1;
if (old_var != NULL)
cvar_free(old_var);
- if (config_write(b, key, NULL, value) < 0) {
- git_strmap_delete(b->values, var->key);
- cvar_free(var);
- return -1;
- }
-
return 0;
}
@@ -1018,6 +1017,7 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
pre_end = post_start = cfg->reader.read_ptr;
git__free(current_section);
+ current_section = NULL;
if (parse_section_header(cfg, ¤t_section) < 0)
goto rewrite_fail;