Make tree cache name a flex-array Signed-off-by: Carlos Martín Nieto <cmn@elego.de>
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
diff --git a/src/tree-cache.c b/src/tree-cache.c
index b3e8a58..9f14312 100644
--- a/src/tree-cache.c
+++ b/src/tree-cache.c
@@ -14,12 +14,7 @@ static int read_tree_internal(git_tree_cache **out,
const char *name_start, *buffer;
int count;
int error = GIT_SUCCESS;
-
- if ((tree = git__malloc(sizeof(git_tree_cache))) == NULL)
- return GIT_ENOMEM;
-
- memset(tree, 0x0, sizeof(git_tree_cache));
- tree->parent = parent;
+ size_t name_len;
buffer = name_start = *buffer_in;
@@ -28,18 +23,22 @@ static int read_tree_internal(git_tree_cache **out,
goto cleanup;
}
- /* NUL-terminated tree name */
- tree->name = git__strdup(name_start);
- if (tree->name == NULL) {
- error = GIT_ENOMEM;
- goto cleanup;
- }
-
if (++buffer >= buffer_end) {
error = GIT_EOBJCORRUPTED;
goto cleanup;
}
+ name_len = strlen(name_start);
+ if ((tree = git__malloc(sizeof(git_tree_cache) + name_len + 1)) == NULL)
+ return GIT_ENOMEM;
+
+ memset(tree, 0x0, sizeof(git_tree_cache));
+ tree->parent = parent;
+
+ /* NUL-terminated tree name */
+ memcpy(tree->name, name_start, name_len);
+ tree->name[name_len] = '\0';
+
/* Blank-terminated ASCII decimal number of entries in this tree */
if (git__strtol32(&count, buffer, &buffer, 10) < GIT_SUCCESS || count < -1) {
error = GIT_EOBJCORRUPTED;
@@ -135,7 +134,6 @@ void git_tree_cache_free(git_tree_cache *tree)
for (i = 0; i < tree->children_count; ++i)
git_tree_cache_free(tree->children[i]);
- free(tree->name);
free(tree->children);
free(tree);
}
diff --git a/src/tree-cache.h b/src/tree-cache.h
index a9e6d2d..1a5d406 100644
--- a/src/tree-cache.h
+++ b/src/tree-cache.h
@@ -12,14 +12,13 @@
#include "git2/oid.h"
struct git_tree_cache {
- char *name;
-
struct git_tree_cache *parent;
struct git_tree_cache **children;
size_t children_count;
ssize_t entries;
git_oid oid;
+ char name[GIT_FLEX_ARRAY];
};
typedef struct git_tree_cache git_tree_cache;