summaryrefslogtreecommitdiff
path: root/src/config_file.c
diff options
context:
space:
mode:
authorCarlos Martín Nieto <cmn@dwim.me>2013-11-02 18:36:34 +0100
committerCarlos Martín Nieto <cmn@dwim.me>2013-11-02 18:54:55 +0100
commita7a64d2cadade53175ba27c1a29023278bdc4288 (patch)
tree8023e83e9cb0d12416f6dfdc5abadc9aa01e7fe4 /src/config_file.c
parent187009e253efa54f6db690a16504adf7f2c32d17 (diff)
downloadlibgit2-a7a64d2cadade53175ba27c1a29023278bdc4288.tar.gz
remote: don't write too much when dealing with multivars
We used to move `data_start` forward, which is wrong as that needs to point to the beginning of the buffer in order to perform size calculations. Introduce a `write_start` variable which indicates where we should start writing from, which is what the `data_start` was being wrongly reused to be.
Diffstat (limited to 'src/config_file.c')
-rw-r--r--src/config_file.c18
1 files changed, 12 insertions, 6 deletions
diff --git a/src/config_file.c b/src/config_file.c
index 6abf60621..c7fc32060 100644
--- a/src/config_file.c
+++ b/src/config_file.c
@@ -1185,7 +1185,7 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
{
int result, c;
int section_matches = 0, last_section_matched = 0, preg_replaced = 0, write_trailer = 0;
- const char *pre_end = NULL, *post_start = NULL, *data_start;
+ const char *pre_end = NULL, *post_start = NULL, *data_start, *write_start;
char *current_section = NULL, *section, *name, *ldot;
git_filebuf file = GIT_FILEBUF_INIT;
struct reader *reader = git_array_get(cfg->readers, 0);
@@ -1207,6 +1207,8 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
return -1; /* OS error when reading the file */
}
+ write_start = data_start;
+
/* Lock the file */
if (git_filebuf_open(&file, cfg->file_path, 0) < 0)
return -1;
@@ -1291,7 +1293,7 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
/* We've found the variable we wanted to change, so
* write anything up to it */
- git_filebuf_write(&file, data_start, pre_end - data_start);
+ git_filebuf_write(&file, write_start, pre_end - write_start);
preg_replaced = 1;
/* Then replace the variable. If the value is NULL, it
@@ -1300,9 +1302,13 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
git_filebuf_printf(&file, "\t%s = %s\n", name, value);
}
- /* multiline variable? we need to keep reading lines to match */
- if (preg != NULL && section_matches) {
- data_start = post_start;
+ /*
+ * If we have a multivar, we should keep looking for entries,
+ * but only if we're in the right section. Otherwise we'll end up
+ * looping on the edge of a matching and a non-matching section.
+ */
+ if (section_matches && preg != NULL) {
+ write_start = post_start;
continue;
}
@@ -1332,7 +1338,7 @@ static int config_write(diskfile_backend *cfg, const char *key, const regex_t *p
git_filebuf_write(&file, post_start, reader->buffer.size - (post_start - data_start));
} else {
if (preg_replaced) {
- git_filebuf_printf(&file, "\n%s", data_start);
+ git_filebuf_printf(&file, "\n%s", write_start);
} else {
git_filebuf_write(&file, reader->buffer.ptr, reader->buffer.size);