From fd0a5a1116a1a4390863886131c4176e38464211 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Thu, 25 Jan 2018 13:01:39 +0100 Subject: [PATCH] decoder/{dsdiff,dsf,mpg123,wavpack}: avoid exceptions in scan methods The scan methods must be "noexcept". --- src/decoder/plugins/DsdiffDecoderPlugin.cxx | 11 ++++++----- src/decoder/plugins/DsfDecoderPlugin.cxx | 8 ++++---- src/decoder/plugins/Mpg123DecoderPlugin.cxx | 7 ++++++- src/decoder/plugins/WavpackDecoderPlugin.cxx | 20 +++++++++++++++++--- 4 files changed, 33 insertions(+), 13 deletions(-) diff --git a/src/decoder/plugins/DsdiffDecoderPlugin.cxx b/src/decoder/plugins/DsdiffDecoderPlugin.cxx index dea3c3a99..c34d303d1 100644 --- a/src/decoder/plugins/DsdiffDecoderPlugin.cxx +++ b/src/decoder/plugins/DsdiffDecoderPlugin.cxx @@ -460,14 +460,15 @@ dsdiff_scan_stream(InputStream &is, if (!dsdiff_read_metadata(nullptr, is, &metadata, &chunk_header)) return false; - auto audio_format = CheckAudioFormat(metadata.sample_rate / 8, - SampleFormat::DSD, - metadata.channels); + const auto sample_rate = metadata.sample_rate / 8; + if (!audio_valid_sample_rate(sample_rate) || + !audio_valid_channel_count(metadata.channels)) + return false; /* calculate song time and add as tag */ - uint64_t n_frames = metadata.chunk_size / audio_format.channels; + uint64_t n_frames = metadata.chunk_size / metadata.channels; auto songtime = SongTime::FromScale(n_frames, - audio_format.sample_rate); + sample_rate); tag_handler_invoke_duration(handler, handler_ctx, songtime); /* Read additional metadata and created tags if available */ diff --git a/src/decoder/plugins/DsfDecoderPlugin.cxx b/src/decoder/plugins/DsfDecoderPlugin.cxx index cef3a5eab..ddb4fa2f4 100644 --- a/src/decoder/plugins/DsfDecoderPlugin.cxx +++ b/src/decoder/plugins/DsfDecoderPlugin.cxx @@ -334,14 +334,14 @@ dsf_scan_stream(InputStream &is, if (!dsf_read_metadata(nullptr, is, &metadata)) return false; - auto audio_format = CheckAudioFormat(metadata.sample_rate / 8, - SampleFormat::DSD, - metadata.channels); + const auto sample_rate = metadata.sample_rate / 8; + if (!audio_valid_sample_rate(sample_rate)) + return false; /* calculate song time and add as tag */ const auto n_blocks = metadata.n_blocks; auto songtime = SongTime::FromScale(n_blocks * DSF_BLOCK_SIZE, - audio_format.sample_rate); + sample_rate); tag_handler_invoke_duration(handler, handler_ctx, songtime); #ifdef ENABLE_ID3TAG diff --git a/src/decoder/plugins/Mpg123DecoderPlugin.cxx b/src/decoder/plugins/Mpg123DecoderPlugin.cxx index 9443a4185..0834f5fee 100644 --- a/src/decoder/plugins/Mpg123DecoderPlugin.cxx +++ b/src/decoder/plugins/Mpg123DecoderPlugin.cxx @@ -292,7 +292,12 @@ mpd_mpg123_scan_file(Path path_fs, } AudioFormat audio_format; - if (!mpd_mpg123_open(handle, path_fs.c_str(), audio_format)) { + try { + if (!mpd_mpg123_open(handle, path_fs.c_str(), audio_format)) { + mpg123_delete(handle); + return false; + } + } catch (...) { mpg123_delete(handle); return false; } diff --git a/src/decoder/plugins/WavpackDecoderPlugin.cxx b/src/decoder/plugins/WavpackDecoderPlugin.cxx index bbd050a42..b444a7f80 100644 --- a/src/decoder/plugins/WavpackDecoderPlugin.cxx +++ b/src/decoder/plugins/WavpackDecoderPlugin.cxx @@ -581,7 +581,14 @@ static bool wavpack_scan_file(Path path_fs, const TagHandler &handler, void *handler_ctx) noexcept { - auto *wpc = WavpackOpenInput(path_fs, OPEN_DSD_FLAG, 0); + WavpackContext *wpc; + + try { + wpc = WavpackOpenInput(path_fs, OPEN_DSD_FLAG, 0); + } catch (...) { + return false; + } + AtScopeExit(wpc) { WavpackCloseFile(wpc); }; @@ -598,8 +605,15 @@ wavpack_scan_stream(InputStream &is, const TagHandler &handler, void *handler_ctx) noexcept { WavpackInput isp(nullptr, is); - auto *wpc = WavpackOpenInput(&mpd_is_reader, &isp, nullptr, - OPEN_DSD_FLAG, 0); + + WavpackContext *wpc; + try { + wpc = WavpackOpenInput(&mpd_is_reader, &isp, nullptr, + OPEN_DSD_FLAG, 0); + } catch (...) { + return false; + } + AtScopeExit(wpc) { WavpackCloseFile(wpc); };