From bba6e3e012e9b51d738b83b363dbcebc731643cb Mon Sep 17 00:00:00 2001 From: Andre Noll Date: Sun, 15 Dec 2013 22:39:21 +0100 Subject: [PATCH] afh: Fix fd leak. The main() function of para_afh obtains the open file descriptor from mmap_full_file() which is then passed to compute_afhi(). If this file descriptor is never closed, we have an fd leak. Unfortunately it depends on the audio format handler whether fd gets closed or not: the mp3 audio format handler closes it through libid3tag's id3_file_close() while all other audio format handlers leave it open. Fix this by changing mp3_get_id3() to operate on a copy of the fd instead so that the original fd remains open. The newly added close() in afh.c thus fixes the fd leak and never closes an invalid fd. --- afh.c | 1 + mp3_afh.c | 14 ++++++++++++-- 2 files changed, 13 insertions(+), 2 deletions(-) diff --git a/afh.c b/afh.c index 49629cb1..f3c25a26 100644 --- a/afh.c +++ b/afh.c @@ -111,6 +111,7 @@ int main(int argc, char **argv) printf("\n"); clear_afhi(&afhi); } + close(fd); ret2 = para_munmap(audio_file_data, audio_file_size); if (ret2 < 0 && ret >= 0) ret = ret2; diff --git a/mp3_afh.c b/mp3_afh.c index cc3f49ff..2506b95e 100644 --- a/mp3_afh.c +++ b/mp3_afh.c @@ -126,9 +126,19 @@ static char *get_strings(struct id3_frame *fr) static void mp3_get_id3(__a_unused unsigned char *map, __a_unused size_t numbytes, int fd, struct taginfo *tags) { - int i; + int i, new_fd; struct id3_tag *id3_t; - struct id3_file *id3_f = id3_file_fdopen(fd, ID3_FILE_MODE_READONLY); + struct id3_file *id3_f; + + /* + * We are not supposed to close fd, but to avoid memory leaks we must + * call id3_file_close() on the id3_file after we are done. As + * id3_file_close() closes fd, we first create a copy for libid3tag. + */ + new_fd = dup(fd); + if (new_fd < 0) + return; + id3_f = id3_file_fdopen(new_fd, ID3_FILE_MODE_READONLY); if (!id3_f) return; -- 2.39.2