Commit d67f270e58942fed5871eca6802d3fa4f290e4f1

Edward Thomson 2015-05-14T13:30:29

index: validate mode of new conflicts

diff --git a/src/index.c b/src/index.c
index b39c089..e37cb1f 100644
--- a/src/index.c
+++ b/src/index.c
@@ -1317,6 +1317,15 @@ int git_index_conflict_add(git_index *index,
 		(ret = index_entry_dup(&entries[2], INDEX_OWNER(index), their_entry)) < 0)
 		goto on_error;
 
+	/* Validate entries */
+	for (i = 0; i < 3; i++) {
+		if (entries[i] && !valid_filemode(entries[i]->mode)) {
+			giterr_set(GITERR_INDEX, "invalid filemode for stage %d entry",
+				i);
+			return -1;
+		}
+	}
+
 	/* Remove existing index entries for each path */
 	for (i = 0; i < 3; i++) {
 		if (entries[i] == NULL)
diff --git a/tests/index/conflicts.c b/tests/index/conflicts.c
index aed5dd1..3b38723 100644
--- a/tests/index/conflicts.c
+++ b/tests/index/conflicts.c
@@ -47,14 +47,17 @@ void test_index_conflicts__add(void)
 	memset(&their_entry, 0x0, sizeof(git_index_entry));
 
 	ancestor_entry.path = "test-one.txt";
+	ancestor_entry.mode = 0100644;
 	ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT);
 	git_oid_fromstr(&ancestor_entry.id, TEST_ANCESTOR_OID);
 
 	our_entry.path = "test-one.txt";
+	our_entry.mode = 0100644;
 	ancestor_entry.flags |= (2 << GIT_IDXENTRY_STAGESHIFT);
 	git_oid_fromstr(&our_entry.id, TEST_OUR_OID);
 
 	their_entry.path = "test-one.txt";
+	their_entry.mode = 0100644;
 	ancestor_entry.flags |= (3 << GIT_IDXENTRY_STAGESHIFT);
 	git_oid_fromstr(&their_entry.id, TEST_THEIR_OID);
 
@@ -75,14 +78,17 @@ void test_index_conflicts__add_fixes_incorrect_stage(void)
 	memset(&their_entry, 0x0, sizeof(git_index_entry));
 
 	ancestor_entry.path = "test-one.txt";
+	ancestor_entry.mode = 0100644;
 	ancestor_entry.flags |= (3 << GIT_IDXENTRY_STAGESHIFT);
 	git_oid_fromstr(&ancestor_entry.id, TEST_ANCESTOR_OID);
 
 	our_entry.path = "test-one.txt";
+	our_entry.mode = 0100644;
 	ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT);
 	git_oid_fromstr(&our_entry.id, TEST_OUR_OID);
 
 	their_entry.path = "test-one.txt";
+	their_entry.mode = 0100644;
 	ancestor_entry.flags |= (2 << GIT_IDXENTRY_STAGESHIFT);
 	git_oid_fromstr(&their_entry.id, TEST_THEIR_OID);
 
@@ -109,8 +115,8 @@ void test_index_conflicts__add_removes_stage_zero(void)
 	memset(&our_entry, 0x0, sizeof(git_index_entry));
 	memset(&their_entry, 0x0, sizeof(git_index_entry));
 
-	staged.mode = 0100644;
 	staged.path = "test-one.txt";
+	staged.mode = 0100644;
 	git_oid_fromstr(&staged.id, TEST_STAGED_OID);
 	cl_git_pass(git_index_add(repo_index, &staged));
 	cl_assert(git_index_entrycount(repo_index) == 9);
@@ -322,6 +328,7 @@ void test_index_conflicts__partial(void)
 	memset(&their_entry, 0x0, sizeof(git_index_entry));
 
 	ancestor_entry.path = "test-one.txt";
+	ancestor_entry.mode = 0100644;
 	ancestor_entry.flags |= (1 << GIT_IDXENTRY_STAGESHIFT);
 	git_oid_fromstr(&ancestor_entry.id, TEST_ANCESTOR_OID);
 
diff --git a/tests/status/worktree.c b/tests/status/worktree.c
index a8d7147..de21a1c 100644
--- a/tests/status/worktree.c
+++ b/tests/status/worktree.c
@@ -461,14 +461,17 @@ void test_status_worktree__conflict_with_diff3(void)
 	memset(&their_entry, 0x0, sizeof(git_index_entry));
 
 	ancestor_entry.path = "modified_file";
+	ancestor_entry.mode = 0100644;
 	git_oid_fromstr(&ancestor_entry.id,
 		"452e4244b5d083ddf0460acf1ecc74db9dcfa11a");
 
 	our_entry.path = "modified_file";
+	our_entry.mode = 0100644;
 	git_oid_fromstr(&our_entry.id,
 		"452e4244b5d083ddf0460acf1ecc74db9dcfa11a");
 
 	their_entry.path = "modified_file";
+	their_entry.mode = 0100644;
 	git_oid_fromstr(&their_entry.id,
 		"452e4244b5d083ddf0460acf1ecc74db9dcfa11a");
 
@@ -614,14 +617,17 @@ void test_status_worktree__conflicted_item(void)
 	memset(&our_entry, 0x0, sizeof(git_index_entry));
 	memset(&their_entry, 0x0, sizeof(git_index_entry));
 
+	ancestor_entry.mode = 0100644;
 	ancestor_entry.path = "modified_file";
 	git_oid_fromstr(&ancestor_entry.id,
 		"452e4244b5d083ddf0460acf1ecc74db9dcfa11a");
 
+	our_entry.mode = 0100644;
 	our_entry.path = "modified_file";
 	git_oid_fromstr(&our_entry.id,
 		"452e4244b5d083ddf0460acf1ecc74db9dcfa11a");
 
+	their_entry.mode = 0100644;
 	their_entry.path = "modified_file";
 	git_oid_fromstr(&their_entry.id,
 		"452e4244b5d083ddf0460acf1ecc74db9dcfa11a");