summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorRussell Belfer <rb@github.com>2012-07-19 10:23:45 -0700
committerRussell Belfer <rb@github.com>2012-07-19 10:23:45 -0700
commit71d273583755c0a2b7f5d608f017f4586add51e4 (patch)
tree9a70607c88d126591a5fc7241987e5d410e3bf0f /src
parent5a8204f8a88c59c2cb07b93930834ef0a5aaf1fc (diff)
downloadlibgit2-71d273583755c0a2b7f5d608f017f4586add51e4.tar.gz
Fix bug with merging diffs with null options
A diff that is created with a NULL options parameter could result in a NULL prefix string, but diff merge was unconditionally strdup'ing it. I added a test to replicate the issue and then a new method that does the right thing with NULL values.
Diffstat (limited to 'src')
-rw-r--r--src/diff.c4
-rw-r--r--src/pool.c5
-rw-r--r--src/pool.h7
3 files changed, 14 insertions, 2 deletions
diff --git a/src/diff.c b/src/diff.c
index 4fea894f8..09f319e6e 100644
--- a/src/diff.c
+++ b/src/diff.c
@@ -814,9 +814,9 @@ int git_diff_merge(
/* prefix strings also come from old pool, so recreate those.*/
onto->opts.old_prefix =
- git_pool_strdup(&onto->pool, onto->opts.old_prefix);
+ git_pool_strdup_safe(&onto->pool, onto->opts.old_prefix);
onto->opts.new_prefix =
- git_pool_strdup(&onto->pool, onto->opts.new_prefix);
+ git_pool_strdup_safe(&onto->pool, onto->opts.new_prefix);
}
git_vector_foreach(&onto_new, i, delta)
diff --git a/src/pool.c b/src/pool.c
index 63bf09cee..64b5c6b00 100644
--- a/src/pool.c
+++ b/src/pool.c
@@ -206,6 +206,11 @@ char *git_pool_strdup(git_pool *pool, const char *str)
return git_pool_strndup(pool, str, strlen(str));
}
+char *git_pool_strdup_safe(git_pool *pool, const char *str)
+{
+ return str ? git_pool_strdup(pool, str) : NULL;
+}
+
char *git_pool_strcat(git_pool *pool, const char *a, const char *b)
{
void *ptr;
diff --git a/src/pool.h b/src/pool.h
index 54a2861ed..05d339244 100644
--- a/src/pool.h
+++ b/src/pool.h
@@ -90,6 +90,13 @@ extern char *git_pool_strndup(git_pool *pool, const char *str, size_t n);
extern char *git_pool_strdup(git_pool *pool, const char *str);
/**
+ * Allocate space and duplicate a string into it, NULL is no error.
+ *
+ * This is allowed only for pools with item_size == sizeof(char)
+ */
+extern char *git_pool_strdup_safe(git_pool *pool, const char *str);
+
+/**
* Allocate space for the concatenation of two strings.
*
* This is allowed only for pools with item_size == sizeof(char)