Commit b183ffe77ee0f81d51019ed46cd10fa0a9a353ac

Carlos Martín Nieto 2011-07-26T12:26:12

Make tree cache name a flex-array Signed-off-by: Carlos Martín Nieto <cmn@elego.de>

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;