diff options
author | Carlos Martín Nieto <cmn@dwim.me> | 2014-05-22 16:01:02 +0200 |
---|---|---|
committer | Carlos Martín Nieto <cmn@dwim.me> | 2014-05-22 16:01:02 +0200 |
commit | 052a2ffde4d8cf0d2b07c70956631d97875ad5ec (patch) | |
tree | 3012d2ca4f8789ab794a3b2e837d315ddc687b57 | |
parent | 9331f98acaffd377a8076ab111bed84ff89e8e72 (diff) | |
download | libgit2-052a2ffde4d8cf0d2b07c70956631d97875ad5ec.tar.gz |
index: check for valid filemodes on addcmn/index-add-modes
-rw-r--r-- | src/index.c | 14 | ||||
-rw-r--r-- | tests/index/filemodes.c | 15 |
2 files changed, 29 insertions, 0 deletions
diff --git a/src/index.c b/src/index.c index 8a7f29279..b63a0bec6 100644 --- a/src/index.c +++ b/src/index.c @@ -1104,6 +1104,15 @@ int git_index_remove_bypath(git_index *index, const char *path) return 0; } +static bool valid_filemode(const int filemode) +{ + return (filemode == GIT_FILEMODE_BLOB || + filemode == GIT_FILEMODE_BLOB_EXECUTABLE || + filemode == GIT_FILEMODE_LINK || + filemode == GIT_FILEMODE_COMMIT); +} + + int git_index_add(git_index *index, const git_index_entry *source_entry) { git_index_entry *entry = NULL; @@ -1111,6 +1120,11 @@ int git_index_add(git_index *index, const git_index_entry *source_entry) assert(index && source_entry && source_entry->path); + if (!valid_filemode(source_entry->mode)) { + giterr_set(GITERR_INDEX, "invalid filemode"); + return -1; + } + if ((ret = index_entry_dup(&entry, source_entry)) < 0 || (ret = index_insert(index, &entry, 1)) < 0) return ret; diff --git a/tests/index/filemodes.c b/tests/index/filemodes.c index 013932696..e00b9c975 100644 --- a/tests/index/filemodes.c +++ b/tests/index/filemodes.c @@ -152,3 +152,18 @@ void test_index_filemodes__trusted(void) git_index_free(index); } + +void test_index_filemodes__invalid(void) +{ + git_index *index; + git_index_entry entry; + + cl_git_pass(git_repository_index(&index, g_repo)); + + entry.path = "foo"; + entry.mode = GIT_OBJ_BLOB; + cl_git_fail(git_index_add(index, &entry)); + + entry.mode = GIT_FILEMODE_BLOB; + cl_git_pass(git_index_add(index, &entry)); +} |