Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 95ae352

Browse files
committed
tree: ensure the entry filename fits in 16 bits
Return an error in case the length is too big. Also take this opportunity to have a single allocating function for the size and overflow logic.
1 parent ee42bb0 commit 95ae352

File tree

1 file changed

+26
-16
lines changed

1 file changed

+26
-16
lines changed

src/tree.c

Lines changed: 26 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -82,47 +82,57 @@ int git_tree_entry_icmp(const git_tree_entry *e1, const git_tree_entry *e2)
8282
}
8383

8484
/**
85-
* Allocate a tree entry, borrowing the filename from the tree which
86-
* owns it. This is useful when reading trees, so we don't allocate a
87-
* ton of small strings but can use the pool.
85+
* Allocate either from the pool or from the system allocator
8886
*/
89-
static git_tree_entry *alloc_entry_pooled(git_pool *pool, const char *filename, size_t filename_len)
87+
static git_tree_entry *alloc_entry_base(git_pool *pool, const char *filename, size_t filename_len)
9088
{
9189
git_tree_entry *entry = NULL;
9290
size_t tree_len;
9391

92+
if (filename_len > UINT16_MAX) {
93+
giterr_set(GITERR_INVALID, "tree entry is over UINT16_MAX in length");
94+
return NULL;
95+
}
96+
9497
if (GIT_ADD_SIZET_OVERFLOW(&tree_len, sizeof(git_tree_entry), filename_len) ||
95-
GIT_ADD_SIZET_OVERFLOW(&tree_len, tree_len, 1) ||
96-
!(entry = git_pool_malloc(pool, tree_len)))
98+
GIT_ADD_SIZET_OVERFLOW(&tree_len, tree_len, 1))
99+
return NULL;
100+
101+
entry = pool ? git_pool_malloc(pool, tree_len) :
102+
git__malloc(tree_len);
103+
if (!entry)
97104
return NULL;
98105

99106
memset(entry, 0x0, sizeof(git_tree_entry));
100107
memcpy(entry->filename, filename, filename_len);
101108
entry->filename[filename_len] = 0;
102109
entry->filename_len = filename_len;
103-
entry->pooled = true;
104110

105111
return entry;
106112
}
107113

108-
static git_tree_entry *alloc_entry(const char *filename)
114+
/**
115+
* Allocate a tree entry, using the poolin the tree which owns
116+
* it. This is useful when reading trees, so we don't allocate a ton
117+
* of small strings but can use the pool.
118+
*/
119+
static git_tree_entry *alloc_entry_pooled(git_pool *pool, const char *filename, size_t filename_len)
109120
{
110121
git_tree_entry *entry = NULL;
111-
size_t filename_len = strlen(filename), tree_len;
112122

113-
if (GIT_ADD_SIZET_OVERFLOW(&tree_len, sizeof(git_tree_entry), filename_len) ||
114-
GIT_ADD_SIZET_OVERFLOW(&tree_len, tree_len, 1) ||
115-
!(entry = git__malloc(tree_len)))
123+
if (!(entry = alloc_entry_base(pool, filename, filename_len)))
116124
return NULL;
117125

118-
memset(entry, 0x0, sizeof(git_tree_entry));
119-
memcpy(entry->filename, filename, filename_len);
120-
entry->filename[filename_len] = 0;
121-
entry->filename_len = filename_len;
126+
entry->pooled = true;
122127

123128
return entry;
124129
}
125130

131+
static git_tree_entry *alloc_entry(const char *filename)
132+
{
133+
return alloc_entry_base(NULL, filename, strlen(filename));
134+
}
135+
126136
struct tree_key_search {
127137
const char *filename;
128138
uint16_t filename_len;

0 commit comments

Comments
 (0)