summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorVicent Marti <tanoku@gmail.com>2010-10-07 00:07:32 +0300
committerVicent Marti <tanoku@gmail.com>2010-10-07 00:14:09 +0300
commitc4b5bedc972d9238db5d6422bc6f4de35b4b67ed (patch)
treeca019aa4c44c8f88e6b7feef1dceff9ec11f5e0c /src
parent2a884588b405c4dee78494119a123fb1878f3490 (diff)
downloadlibgit2-c4b5bedc972d9238db5d6422bc6f4de35b4b67ed.tar.gz
Fix possible segfaults in src/tree.c (issue 1)
git_tree_entry_byname was dereferencing a NULL pointer when the searched file couldn't be found on the tree. New test cases have been added to check for entry access methods. Signed-off-by: Vicent Marti <tanoku@gmail.com>
Diffstat (limited to 'src')
-rw-r--r--src/tree.c21
1 files changed, 20 insertions, 1 deletions
diff --git a/src/tree.c b/src/tree.c
index 43a50423d..b809290b2 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -124,26 +124,36 @@ unsigned int git_tree_entry_attributes(git_tree_entry *entry)
const char *git_tree_entry_name(git_tree_entry *entry)
{
+ assert(entry);
return entry->filename;
}
const git_oid *git_tree_entry_id(git_tree_entry *entry)
{
+ assert(entry);
return &entry->oid;
}
git_object *git_tree_entry_2object(git_tree_entry *entry)
{
+ assert(entry);
return git_repository_lookup(entry->owner->object.repo, &entry->oid, GIT_OBJ_ANY);
}
git_tree_entry *git_tree_entry_byname(git_tree *tree, const char *filename)
{
- return *(git_tree_entry **)bsearch(filename, tree->entries, tree->entry_count, sizeof(git_tree_entry *), entry_cmp);
+ git_tree_entry **found;
+
+ assert(tree && filename);
+
+ found = bsearch(filename, tree->entries, tree->entry_count, sizeof(git_tree_entry *), entry_cmp);
+ return found ? *found : NULL;
}
git_tree_entry *git_tree_entry_byindex(git_tree *tree, int idx)
{
+ assert(tree);
+
if (tree->entries == NULL)
return NULL;
@@ -152,6 +162,7 @@ git_tree_entry *git_tree_entry_byindex(git_tree *tree, int idx)
size_t git_tree_entrycount(git_tree *tree)
{
+ assert(tree);
return tree->entry_count;
}
@@ -159,6 +170,8 @@ void git_tree_add_entry(git_tree *tree, const git_oid *id, const char *filename,
{
git_tree_entry *entry;
+ assert(tree && id && filename);
+
if (tree->entry_count >= tree->array_size)
resize_tree_array(tree);
@@ -182,6 +195,8 @@ int git_tree_remove_entry_byindex(git_tree *tree, int idx)
{
git_tree_entry *remove_ptr;
+ assert(tree);
+
if (idx < 0 || idx >= (int)tree->entry_count)
return GIT_ENOTFOUND;
@@ -200,6 +215,8 @@ int git_tree_remove_entry_byname(git_tree *tree, const char *filename)
git_tree_entry **entry_ptr;
int idx;
+ assert(tree && filename);
+
entry_ptr = bsearch(filename, tree->entries, tree->entry_count, sizeof(git_tree_entry *), entry_cmp);
if (entry_ptr == NULL)
return GIT_ENOTFOUND;
@@ -212,6 +229,8 @@ int git_tree__writeback(git_tree *tree, git_odb_source *src)
{
size_t i;
+ assert(tree && src);
+
if (tree->entries == NULL)
return GIT_ERROR;