Fix off-by-one error in removal of cache entry.

Also make the return value of "cache_name_pos()" be sane: positive
or zero if we found it (it's the index into the cache array), and
"-pos-1" to indicate where it should go if we didn't.
maint
Linus Torvalds 2005-04-10 22:06:50 -07:00
parent 262e82b4a7
commit 76e7f4ec48
1 changed files with 7 additions and 7 deletions

View File

@ -276,24 +276,23 @@ int cache_name_pos(const char *name, int namelen)
struct cache_entry *ce = active_cache[next]; struct cache_entry *ce = active_cache[next];
int cmp = cache_name_compare(name, namelen, ce->name, ce->namelen); int cmp = cache_name_compare(name, namelen, ce->name, ce->namelen);
if (!cmp) if (!cmp)
return -next-1; return next;
if (cmp < 0) { if (cmp < 0) {
last = next; last = next;
continue; continue;
} }
first = next+1; first = next+1;
} }
return first; return -first-1;
} }


int remove_file_from_cache(char *path) int remove_file_from_cache(char *path)
{ {
int pos = cache_name_pos(path, strlen(path)); int pos = cache_name_pos(path, strlen(path));
if (pos < 0) { if (pos >= 0) {
pos = -pos-1;
active_nr--; active_nr--;
if (pos < active_nr) if (pos < active_nr)
memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos - 1) * sizeof(struct cache_entry *)); memmove(active_cache + pos, active_cache + pos + 1, (active_nr - pos) * sizeof(struct cache_entry *));
} }
return 0; return 0;
} }
@ -305,10 +304,11 @@ int add_cache_entry(struct cache_entry *ce, int ok_to_add)
pos = cache_name_pos(ce->name, ce->namelen); pos = cache_name_pos(ce->name, ce->namelen);


/* existing match? Just replace it */ /* existing match? Just replace it */
if (pos < 0) { if (pos >= 0) {
active_cache[-pos-1] = ce; active_cache[pos] = ce;
return 0; return 0;
} }
pos = -pos-1;


if (!ok_to_add) if (!ok_to_add)
return -1; return -1;