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.
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
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;
}