From 6f8e84b60559c75ab36aa907381e7b6f31edf1ef Mon Sep 17 00:00:00 2001 From: Andy Delcambre Date: Fri, 22 Aug 2014 13:56:46 -0700 Subject: [PATCH 1/2] Type check the tree value for Rugged::Index#read_tree libgit2 does no type checking in `git_index_read_tree` which means that `index.read_tree` will segfault if you pass something other than a tree. This ensures that the value passed is actually a tree. --- ext/rugged/rugged_index.c | 4 ++++ test/index_test.rb | 10 ++++++++++ 2 files changed, 14 insertions(+) diff --git a/ext/rugged/rugged_index.c b/ext/rugged/rugged_index.c index bf0922bf3..f13381bd7 100644 --- a/ext/rugged/rugged_index.c +++ b/ext/rugged/rugged_index.c @@ -681,6 +681,10 @@ static VALUE rb_git_index_readtree(VALUE self, VALUE rb_tree) Data_Get_Struct(self, git_index, index); Data_Get_Struct(rb_tree, git_tree, tree); + if (!rb_obj_is_kind_of(rb_tree, rb_cRuggedTree)) { + rb_raise(rb_eTypeError, "A Rugged::Tree instance is required"); + } + error = git_index_read_tree(index, tree); rugged_exception_check(error); diff --git a/test/index_test.rb b/test/index_test.rb index e7565d67d..38d41684f 100644 --- a/test/index_test.rb +++ b/test/index_test.rb @@ -332,6 +332,16 @@ def test_build_tree_from_index assert head_sha != new_tree_sha assert_nil @repo.lookup(new_tree_sha)['second.txt'] end + + def test_read_tree_with_not_a_tree + head_sha = @repo.references['refs/remotes/origin/packed'].resolve.target_id + commit = @repo.lookup(head_sha) + + index = @repo.index + assert_raises TypeError do + index.read_tree(commit) + end + end end class IndexAddAllTest < Rugged::SandboxedTestCase From 733925085f3cfc329f21787ddd826b042e007131 Mon Sep 17 00:00:00 2001 From: Andy Delcambre Date: Fri, 22 Aug 2014 14:11:08 -0700 Subject: [PATCH 2/2] Tabs not spaces --- ext/rugged/rugged_index.c | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/ext/rugged/rugged_index.c b/ext/rugged/rugged_index.c index f13381bd7..705c87e8a 100644 --- a/ext/rugged/rugged_index.c +++ b/ext/rugged/rugged_index.c @@ -681,9 +681,9 @@ static VALUE rb_git_index_readtree(VALUE self, VALUE rb_tree) Data_Get_Struct(self, git_index, index); Data_Get_Struct(rb_tree, git_tree, tree); - if (!rb_obj_is_kind_of(rb_tree, rb_cRuggedTree)) { - rb_raise(rb_eTypeError, "A Rugged::Tree instance is required"); - } + if (!rb_obj_is_kind_of(rb_tree, rb_cRuggedTree)) { + rb_raise(rb_eTypeError, "A Rugged::Tree instance is required"); + } error = git_index_read_tree(index, tree); rugged_exception_check(error);