Recently Written · git

sad

sad (2f30.org simple audio daemon) with a Cygwin/OSS port on branch cygwin

git clone https://github.com/equwal/sad

Log | Files | Refs


commit 093d0682d14384b3ab8cb114d211c42dd4a03445
sin <sin@2f30.org>
2015-01-02 15:53:47 +0000

Fix memory leak + size calculations when removing an element from the playlist

 playlist.c | 12 ++++++++----
 1 file changed, 8 insertions(+), 4 deletions(-)
diff --git a/playlist.c b/playlist.c
index cd0f663..8f77e3d 100644
--- a/playlist.c
+++ b/playlist.c
@@ -42,14 +42,18 @@ rmplaylist(int id)
 	Song *s;
 	int   i;
 
-	for (i = 0; i < playlist.nsongs; i++)
-		if (playlist.songs[i]->id == id)
+	for (i = 0; i < playlist.nsongs; i++) {
+		s = playlist.songs[i];
+		if (s->id == id)
 			break;
+	}
 	if (i == playlist.nsongs)
 		return -1;
+
+	free(s);
 	s = getnextsong();
-	memmove(&playlist.songs[i], &playlist.songs[i+1],
-	        (playlist.nsongs - i - 1) * sizeof(*playlist.songs[i]));
+	memmove(&playlist.songs[i], &playlist.songs[i + 1],
+	        (playlist.nsongs - i - 1) * sizeof(playlist.songs[i]));
 	putcursong(s);
 	playlist.nsongs--;
 	return 0;