Skip to content

Commit 8d87e33

Browse files
derrickstoleegitster
authored andcommitted
cache-tree: simplify verify_cache() prototype
The verify_cache() method takes an array of cache entries and a count, but these are always provided directly from a struct index_state. Use a pointer to the full structure instead. There is a subtle point when istate->cache_nr is zero that subtracting one will underflow. This triggers a failure in t0000-basic.sh, among others. Use "i + 1 < istate->cache_nr" to avoid these strange comparisons. Convert i to be unsigned as well, which also removes the potential signed overflow in the unlikely case that cache_nr is over 2.1 billion entries. The 'funny' variable has a maximum value of 11, so making it unsigned does not change anything of importance. Signed-off-by: Derrick Stolee <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent fb08826 commit 8d87e33

File tree

1 file changed

+8
-9
lines changed

1 file changed

+8
-9
lines changed

cache-tree.c

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,16 +151,15 @@ void cache_tree_invalidate_path(struct index_state *istate, const char *path)
151151
istate->cache_changed |= CACHE_TREE_CHANGED;
152152
}
153153

154-
static int verify_cache(struct cache_entry **cache,
155-
int entries, int flags)
154+
static int verify_cache(struct index_state *istate, int flags)
156155
{
157-
int i, funny;
156+
unsigned i, funny;
158157
int silent = flags & WRITE_TREE_SILENT;
159158

160159
/* Verify that the tree is merged */
161160
funny = 0;
162-
for (i = 0; i < entries; i++) {
163-
const struct cache_entry *ce = cache[i];
161+
for (i = 0; i < istate->cache_nr; i++) {
162+
const struct cache_entry *ce = istate->cache[i];
164163
if (ce_stage(ce)) {
165164
if (silent)
166165
return -1;
@@ -180,13 +179,13 @@ static int verify_cache(struct cache_entry **cache,
180179
* stage 0 entries.
181180
*/
182181
funny = 0;
183-
for (i = 0; i < entries - 1; i++) {
182+
for (i = 0; i + 1 < istate->cache_nr; i++) {
184183
/* path/file always comes after path because of the way
185184
* the cache is sorted. Also path can appear only once,
186185
* which means conflicting one would immediately follow.
187186
*/
188-
const struct cache_entry *this_ce = cache[i];
189-
const struct cache_entry *next_ce = cache[i + 1];
187+
const struct cache_entry *this_ce = istate->cache[i];
188+
const struct cache_entry *next_ce = istate->cache[i + 1];
190189
const char *this_name = this_ce->name;
191190
const char *next_name = next_ce->name;
192191
int this_len = ce_namelen(this_ce);
@@ -438,7 +437,7 @@ int cache_tree_update(struct index_state *istate, int flags)
438437
{
439438
int skip, i;
440439

441-
i = verify_cache(istate->cache, istate->cache_nr, flags);
440+
i = verify_cache(istate, flags);
442441

443442
if (i)
444443
return i;

0 commit comments

Comments
 (0)