aboutsummaryrefslogtreecommitdiffstats
path: root/src/songvec.c
diff options
context:
space:
mode:
authorEric Wong <normalperson@yhbt.net>2008-10-14 11:10:00 +0200
committerMax Kellermann <max@duempel.org>2008-10-14 11:10:00 +0200
commit06c8a9ed0b1a01343d21859bff22ae67f4b29021 (patch)
treeef6fb6b9d77f6d83bd0ef35d176f5475bb218550 /src/songvec.c
parentf71ac4d479502738682f2328a6523d18be705ac9 (diff)
downloadmpd-06c8a9ed0b1a01343d21859bff22ae67f4b29021.tar.gz
mpd-06c8a9ed0b1a01343d21859bff22ae67f4b29021.tar.xz
mpd-06c8a9ed0b1a01343d21859bff22ae67f4b29021.zip
{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...
Diffstat (limited to 'src/songvec.c')
-rw-r--r--src/songvec.c13
1 files changed, 6 insertions, 7 deletions
diff --git a/src/songvec.c b/src/songvec.c
index 80954643e..a62c6e671 100644
--- a/src/songvec.c
+++ b/src/songvec.c
@@ -47,28 +47,27 @@ songvec_find(const struct songvec *sv, const char *url)
int
songvec_delete(struct songvec *sv, const struct song *del)
{
- int i;
+ size_t i;
pthread_mutex_lock(&nr_lock);
- for (i = sv->nr; --i >= 0; ) {
+ for (i = 0; i < sv->nr; ++i) {
if (sv->base[i] != del)
continue;
/* we _don't_ call song_free() here */
if (!--sv->nr) {
- pthread_mutex_unlock(&nr_lock);
free(sv->base);
sv->base = NULL;
- return i;
} else {
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));
}
- break;
+ pthread_mutex_unlock(&nr_lock);
+ return i;
}
pthread_mutex_unlock(&nr_lock);
- return i;
+ return -1; /* not found */
}
void