Commit 9de27ad0c453f98844cc91ce4e39d338b1fb8338

Sakari Jokinen 2011-02-25T19:05:29

Check for valid range of attributes for tree entry

diff --git a/include/git2/tree.h b/include/git2/tree.h
index 70040f0..a3c6012 100644
--- a/include/git2/tree.h
+++ b/include/git2/tree.h
@@ -215,7 +215,7 @@ GIT_EXTERN(void) git_tree_entry_set_name(git_tree_entry *entry, const char *name
  * @param entry Entry object which will be modified
  * @param oid new attributes for the entry
  */
-GIT_EXTERN(void) git_tree_entry_set_attributes(git_tree_entry *entry, int attr);
+GIT_EXTERN(int) git_tree_entry_set_attributes(git_tree_entry *entry, int attr);
 
 /** @} */
 GIT_END_DECL
diff --git a/src/tree.c b/src/tree.c
index 30938f2..16824f9 100644
--- a/src/tree.c
+++ b/src/tree.c
@@ -31,6 +31,8 @@
 #include "git2/object.h"
 
 #define DEFAULT_TREE_SIZE 16
+#define MAX_FILEMODE 0777777
+#define MAX_FILEMODE_BYTES 6
 
 int entry_search_cmp(const void *key, const void *array_member)
 {
@@ -40,6 +42,10 @@ int entry_search_cmp(const void *key, const void *array_member)
 	return strcmp(filename, entry->filename);
 }
 
+static int valid_attributes(const int attributes) {
+	return attributes >= 0 && attributes <= MAX_FILEMODE; 
+}
+
 int entry_sort_cmp(const void *a, const void *b)
 {
 	const git_tree_entry *entry_a = *(const git_tree_entry **)(a);
@@ -101,12 +107,17 @@ const git_oid *git_tree_id(git_tree *c)
 	return git_object_id((git_object *)c);
 }
 
-void git_tree_entry_set_attributes(git_tree_entry *entry, int attr)
+int git_tree_entry_set_attributes(git_tree_entry *entry, int attr)
 {
 	assert(entry && entry->owner);
+	
+	if (!valid_attributes(attr)) {
+		return GIT_ERROR;
+	}
 
 	entry->attr = attr;
 	entry->owner->object.modified = 1;
+	return GIT_SUCCESS;
 }
 
 void git_tree_entry_set_name(git_tree_entry *entry, const char *name)
@@ -190,6 +201,9 @@ int git_tree_add_entry(git_tree_entry **entry_out, git_tree *tree, const git_oid
 	git_tree_entry *entry;
 
 	assert(tree && id && filename);
+	if (!valid_attributes(attributes)) {
+	  return GIT_ERROR;
+	}
 
 	if ((entry = git__malloc(sizeof(git_tree_entry))) == NULL)
 		return GIT_ENOMEM;
@@ -249,7 +263,7 @@ int git_tree_remove_entry_byname(git_tree *tree, const char *filename)
 int git_tree__writeback(git_tree *tree, git_odb_source *src)
 {
 	size_t i;
-	char filemode[8];
+	char filemode[MAX_FILEMODE_BYTES + 1 + 1];
 
 	assert(tree && src);
 
@@ -263,8 +277,8 @@ int git_tree__writeback(git_tree *tree, git_odb_source *src)
 
 		entry = git_vector_get(&tree->entries, i);
 	
-		sprintf(filemode, "%o ", entry->attr);
-
+		snprintf(filemode, sizeof(filemode), "%o ", entry->attr);
+		  
 		git__source_write(src, filemode, strlen(filemode));
 		git__source_write(src, entry->filename, strlen(entry->filename) + 1);
 		git__source_write(src, entry->oid.id, GIT_OID_RAWSZ);