{dir,song}vec: fix off-by-one errors in {dir,song}vec_delete

Found by Valgrind while looking for another bug...

Hmm.. I should really just make this code generic since
they're duplicated...
This commit is contained in:
Eric Wong 2008-10-14 11:10:00 +02:00 committed by Max Kellermann
parent f71ac4d479
commit 06c8a9ed0b
2 changed files with 9 additions and 10 deletions

View File

@ -37,9 +37,9 @@ struct directory *dirvec_find(const struct dirvec *dv, const char *path)
int dirvec_delete(struct dirvec *dv, struct directory *del) int dirvec_delete(struct dirvec *dv, struct directory *del)
{ {
int i; size_t i;
for (i = dv->nr; --i >= 0; ) { for (i = 0; i < dv->nr; ++i) {
if (dv->base[i] != del) if (dv->base[i] != del)
continue; continue;
/* we _don't_ call directory_free() here */ /* we _don't_ call directory_free() here */
@ -48,7 +48,7 @@ int dirvec_delete(struct dirvec *dv, struct directory *del)
dv->base = NULL; dv->base = NULL;
} else { } else {
memmove(&dv->base[i], &dv->base[i + 1], memmove(&dv->base[i], &dv->base[i + 1],
(dv->nr - i + 1) * sizeof(struct directory *)); (dv->nr - i) * sizeof(struct directory *));
dv->base = xrealloc(dv->base, dv_size(dv)); dv->base = xrealloc(dv->base, dv_size(dv));
} }
return i; return i;

View File

@ -47,28 +47,27 @@ songvec_find(const struct songvec *sv, const char *url)
int int
songvec_delete(struct songvec *sv, const struct song *del) songvec_delete(struct songvec *sv, const struct song *del)
{ {
int i; size_t i;
pthread_mutex_lock(&nr_lock); pthread_mutex_lock(&nr_lock);
for (i = sv->nr; --i >= 0; ) { for (i = 0; i < sv->nr; ++i) {
if (sv->base[i] != del) if (sv->base[i] != del)
continue; continue;
/* we _don't_ call song_free() here */ /* we _don't_ call song_free() here */
if (!--sv->nr) { if (!--sv->nr) {
pthread_mutex_unlock(&nr_lock);
free(sv->base); free(sv->base);
sv->base = NULL; sv->base = NULL;
return i;
} else { } else {
memmove(&sv->base[i], &sv->base[i + 1], memmove(&sv->base[i], &sv->base[i + 1],
(sv->nr - i + 1) * sizeof(struct song *)); (sv->nr - i) * sizeof(struct song *));
sv->base = xrealloc(sv->base, sv_size(sv)); sv->base = xrealloc(sv->base, sv_size(sv));
} }
break; pthread_mutex_unlock(&nr_lock);
return i;
} }
pthread_mutex_unlock(&nr_lock); pthread_mutex_unlock(&nr_lock);
return i; return -1; /* not found */
} }
void void