Commit 29f498e0fd0b4f1001ffed209042d8996956e6aa

Patrick Steinhardt 2017-05-19T13:38:34

index: move index entry size computation into its own function Create a new function `index_entry_size` which encapsulates the logic to calculate how much space is needed for an index entry, whether it is simple/extended or compressed/uncompressed. This can later be re-used by our code writing index entries.

diff --git a/src/index.c b/src/index.c
index aaf76c8..161a9da 100644
--- a/src/index.c
+++ b/src/index.c
@@ -2282,6 +2282,21 @@ out_err:
 	return 0;
 }
 
+static size_t index_entry_size(size_t path_len, size_t varint_len, uint32_t flags)
+{
+	if (varint_len) {
+		if (flags & GIT_IDXENTRY_EXTENDED)
+			return offsetof(struct entry_long, path) + path_len + 1 + varint_len;
+		else
+			return offsetof(struct entry_short, path) + path_len + 1 + varint_len;
+	} else {
+		if (flags & GIT_IDXENTRY_EXTENDED)
+			return long_entry_size(path_len);
+		else
+			return short_entry_size(path_len);
+	}
+}
+
 static size_t read_entry(
 	git_index_entry **out,
 	git_index *index,
@@ -2344,10 +2359,7 @@ static size_t read_entry(
 			path_length = path_end - path_ptr;
 		}
 
-		if (entry.flags & GIT_IDXENTRY_EXTENDED)
-			entry_size = long_entry_size(path_length);
-		else
-			entry_size = short_entry_size(path_length);
+		entry_size = index_entry_size(path_length, 0, entry.flags);
 
 		if (INDEX_FOOTER_SIZE + entry_size > buffer_size)
 			return 0;
@@ -2372,7 +2384,7 @@ static size_t read_entry(
 
 		memcpy(tmp_path, last, prefix_len);
 		memcpy(tmp_path + prefix_len, path_ptr + varint_len, suffix_len + 1);
-		entry_size = long_entry_size(prefix_len + suffix_len);
+		entry_size = index_entry_size(suffix_len, varint_len, entry.flags);
 		entry.path = tmp_path;
 	}