Check for valid range of attributes for tree entry
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
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);