*/
#include "tree-cache.h"
+
#include "pool.h"
#include "tree.h"
return -1;
/* Blank-terminated ASCII decimal number of entries in this tree */
- if (git__strtol32(&count, buffer, &buffer, 10) < 0)
+ if (git__strntol32(&count, buffer, buffer_end - buffer, &buffer, 10) < 0)
goto corrupted;
tree->entry_count = count;
goto corrupted;
/* Number of children of the tree, newline-terminated */
- if (git__strtol32(&count, buffer, &buffer, 10) < 0 || count < 0)
+ if (git__strntol32(&count, buffer, buffer_end - buffer, &buffer, 10) < 0 || count < 0)
goto corrupted;
tree->children_count = count;
/* Parse children: */
if (tree->children_count > 0) {
- unsigned int i;
+ size_t i, bufsize;
+
+ GIT_ERROR_CHECK_ALLOC_MULTIPLY(&bufsize, tree->children_count, sizeof(git_tree_cache*));
- tree->children = git_pool_malloc(pool, tree->children_count * sizeof(git_tree_cache *));
- GITERR_CHECK_ALLOC(tree->children);
+ tree->children = git_pool_malloc(pool, bufsize);
+ GIT_ERROR_CHECK_ALLOC(tree->children);
- memset(tree->children, 0x0, tree->children_count * sizeof(git_tree_cache *));
+ memset(tree->children, 0x0, bufsize);
for (i = 0; i < tree->children_count; ++i) {
if (read_tree_internal(&tree->children[i], &buffer, buffer_end, pool) < 0)
return 0;
corrupted:
- giterr_set(GITERR_INDEX, "Corrupted TREE extension in index");
+ git_error_set(GIT_ERROR_INDEX, "corrupted TREE extension in index");
return -1;
}
return -1;
if (buffer < buffer_end) {
- giterr_set(GITERR_INDEX, "Corrupted TREE extension in index (unexpected trailing data)");
+ git_error_set(GIT_ERROR_INDEX, "corrupted TREE extension in index (unexpected trailing data)");
return -1;
}
static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_pool *pool)
{
git_repository *repo;
- size_t i, j, nentries, ntrees;
+ size_t i, j, nentries, ntrees, alloc_size;
int error;
repo = git_tree_owner(tree);
ntrees++;
}
+ GIT_ERROR_CHECK_ALLOC_MULTIPLY(&alloc_size, ntrees, sizeof(git_tree_cache *));
+
cache->children_count = ntrees;
- cache->children = git_pool_mallocz(pool, ntrees * sizeof(git_tree_cache *));
- GITERR_CHECK_ALLOC(cache->children);
+ cache->children = git_pool_mallocz(pool, alloc_size);
+ GIT_ERROR_CHECK_ALLOC(cache->children);
j = 0;
for (i = 0; i < nentries; i++) {
int git_tree_cache_new(git_tree_cache **out, const char *name, git_pool *pool)
{
- size_t name_len;
+ size_t name_len, alloc_size;
git_tree_cache *tree;
name_len = strlen(name);
- tree = git_pool_malloc(pool, sizeof(git_tree_cache) + name_len + 1);
- GITERR_CHECK_ALLOC(tree);
+
+ GIT_ERROR_CHECK_ALLOC_ADD3(&alloc_size, sizeof(git_tree_cache), name_len, 1);
+
+ tree = git_pool_malloc(pool, alloc_size);
+ GIT_ERROR_CHECK_ALLOC(tree);
memset(tree, 0x0, sizeof(git_tree_cache));
/* NUL-terminated tree name */