@@ -120,12 +120,19 @@ static int read_tree_internal(git_tree_cache **out,
120120
121121 /* Parse children: */
122122 if (tree -> children_count > 0 ) {
123- unsigned int i ;
123+ size_t i ;
124+ uint32_t bufsize ;
124125
125- tree -> children = git_pool_malloc (pool , tree -> children_count * sizeof (git_tree_cache * ));
126+ if (tree -> children_count > UINT32_MAX / sizeof (git_tree_cache * )) {
127+ git_error_set_oom ();
128+ return -1 ;
129+ }
130+
131+ bufsize = (uint32_t )(tree -> children_count * sizeof (git_tree_cache * ));
132+ tree -> children = git_pool_malloc (pool , bufsize );
126133 GIT_ERROR_CHECK_ALLOC (tree -> children );
127134
128- memset (tree -> children , 0x0 , tree -> children_count * sizeof ( git_tree_cache * ) );
135+ memset (tree -> children , 0x0 , bufsize );
129136
130137 for (i = 0 ; i < tree -> children_count ; ++ i ) {
131138 if (read_tree_internal (& tree -> children [i ], & buffer , buffer_end , pool ) < 0 )
@@ -182,8 +189,13 @@ static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_
182189 ntrees ++ ;
183190 }
184191
192+ if (ntrees > UINT32_MAX / sizeof (git_tree_cache * )) {
193+ git_error_set_oom ();
194+ return -1 ;
195+ }
196+
185197 cache -> children_count = ntrees ;
186- cache -> children = git_pool_mallocz (pool , ntrees * sizeof (git_tree_cache * ));
198+ cache -> children = git_pool_mallocz (pool , ( uint32_t )( ntrees * sizeof (git_tree_cache * ) ));
187199 GIT_ERROR_CHECK_ALLOC (cache -> children );
188200
189201 j = 0 ;
@@ -232,11 +244,19 @@ int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_poo
232244
233245int git_tree_cache_new (git_tree_cache * * out , const char * name , git_pool * pool )
234246{
235- size_t name_len ;
247+ size_t name_len , alloc_size ;
236248 git_tree_cache * tree ;
237249
238250 name_len = strlen (name );
239- tree = git_pool_malloc (pool , sizeof (git_tree_cache ) + name_len + 1 );
251+
252+ GIT_ERROR_CHECK_ALLOC_ADD3 (& alloc_size , sizeof (git_tree_cache ), name_len , 1 );
253+
254+ if (alloc_size > UINT32_MAX ) {
255+ git_error_set_oom ();
256+ return -1 ;
257+ }
258+
259+ tree = git_pool_malloc (pool , (uint32_t )alloc_size );
240260 GIT_ERROR_CHECK_ALLOC (tree );
241261
242262 memset (tree , 0x0 , sizeof (git_tree_cache ));
0 commit comments