]> git.proxmox.com Git - libgit2.git/blobdiff - src/tree-cache.c
New upstream version 1.1.0+dfsg.1
[libgit2.git] / src / tree-cache.c
index b37be0f0d4996121bacbc111dc97be9ccc4882b1..04d86fd3691dfcf52b162b4a5dfc91af310c99fb 100644 (file)
@@ -6,6 +6,7 @@
  */
 
 #include "tree-cache.h"
+
 #include "pool.h"
 #include "tree.h"
 
@@ -90,7 +91,7 @@ static int read_tree_internal(git_tree_cache **out,
                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;
@@ -99,7 +100,7 @@ static int read_tree_internal(git_tree_cache **out,
                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;
@@ -119,12 +120,14 @@ static int read_tree_internal(git_tree_cache **out,
 
        /* 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)
@@ -137,7 +140,7 @@ static int read_tree_internal(git_tree_cache **out,
        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;
 }
 
@@ -149,7 +152,7 @@ int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer
                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;
        }
 
@@ -159,7 +162,7 @@ int git_tree_cache_read(git_tree_cache **tree, const char *buffer, size_t buffer
 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);
@@ -181,9 +184,11 @@ static int read_tree_recursive(git_tree_cache *cache, const git_tree *tree, git_
                        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++) {
@@ -231,12 +236,15 @@ int git_tree_cache_read_tree(git_tree_cache **out, const git_tree *tree, git_poo
 
 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 */