diff options
author | Robin Gareus <robin@gareus.org> | 2019-08-12 15:02:24 +0200 |
---|---|---|
committer | Robin Gareus <robin@gareus.org> | 2019-08-12 15:02:24 +0200 |
commit | 98c65406e3e17bc70840cc531424e0c65521b96b (patch) | |
tree | 343448adef0c66c8b0aae2ceac1f7881ee1a3a8c /libs/audiographer | |
parent | b48a3a6abb9925b1a999755db79a0ea4ddd1a7fe (diff) |
Prefer std::vector<> over C-style malloc/free**
This fixes a potential out of bounds read `if (_dbtp_plugin[c])`
if c is larger than the allocated channel count.
Diffstat (limited to 'libs/audiographer')
-rw-r--r-- | libs/audiographer/audiographer/general/loudness_reader.h | 6 | ||||
-rw-r--r-- | libs/audiographer/src/general/analyser.cc | 10 | ||||
-rw-r--r-- | libs/audiographer/src/general/loudness_reader.cc | 57 |
3 files changed, 33 insertions, 40 deletions
diff --git a/libs/audiographer/audiographer/general/loudness_reader.h b/libs/audiographer/audiographer/general/loudness_reader.h index 0ee4b5bae0..94643c2133 100644 --- a/libs/audiographer/audiographer/general/loudness_reader.h +++ b/libs/audiographer/audiographer/general/loudness_reader.h @@ -19,6 +19,8 @@ #ifndef AUDIOGRAPHER_LOUDNESS_READER_H #define AUDIOGRAPHER_LOUDNESS_READER_H +#include <vector> + #include <vamp-hostsdk/PluginLoader.h> #include "audiographer/visibility.h" @@ -47,8 +49,8 @@ class LIBAUDIOGRAPHER_API LoudnessReader : public ListedSource<float>, public Si using Sink<float>::process; protected: - Vamp::Plugin* _ebur_plugin; - Vamp::Plugin** _dbtp_plugin; + Vamp::Plugin* _ebur_plugin; + std::vector<Vamp::Plugin*> _dbtp_plugins; float _sample_rate; unsigned int _channels; diff --git a/libs/audiographer/src/general/analyser.cc b/libs/audiographer/src/general/analyser.cc index d38db2dff0..930a2a95ab 100644 --- a/libs/audiographer/src/general/analyser.cc +++ b/libs/audiographer/src/general/analyser.cc @@ -149,12 +149,11 @@ Analyser::process (ProcessContext<float> const & ctx) } float const * const data = ctx.data (); - for (unsigned int c = 0; c < _channels; ++c) { - if (!_dbtp_plugin[c]) { continue; } + for (unsigned int c = 0; c < _channels, c < _dbtp_plugins.size (); ++c) { for (s = 0; s < n_samples; ++s) { _bufs[0][s] = data[s * _channels + c]; } - _dbtp_plugin[c]->process (_bufs, Vamp::RealTime::fromSeconds ((double) _pos / _sample_rate)); + _dbtp_plugins.at(c)->process (_bufs, Vamp::RealTime::fromSeconds ((double) _pos / _sample_rate)); } fftwf_execute (_fft_plan); @@ -248,9 +247,8 @@ Analyser::result () } const unsigned cmask = _result.n_channels - 1; // [0, 1] - for (unsigned int c = 0; c < _channels; ++c) { - if (!_dbtp_plugin[c]) { continue; } - Vamp::Plugin::FeatureSet features = _dbtp_plugin[c]->getRemainingFeatures (); + for (unsigned int c = 0; c < _channels, c < _dbtp_plugins.size (); ++c) { + Vamp::Plugin::FeatureSet features = _dbtp_plugins.at(c)->getRemainingFeatures (); if (!features.empty () && features.size () == 2) { _result.have_dbtp = true; float p = features[0][0].values[0]; diff --git a/libs/audiographer/src/general/loudness_reader.cc b/libs/audiographer/src/general/loudness_reader.cc index 22902625cd..495d653d79 100644 --- a/libs/audiographer/src/general/loudness_reader.cc +++ b/libs/audiographer/src/general/loudness_reader.cc @@ -24,7 +24,6 @@ using namespace AudioGrapher; LoudnessReader::LoudnessReader (float sample_rate, unsigned int channels, samplecnt_t bufsize) : _ebur_plugin (0) - , _dbtp_plugin (0) , _sample_rate (sample_rate) , _channels (channels) , _bufsize (bufsize / channels) @@ -47,16 +46,15 @@ LoudnessReader::LoudnessReader (float sample_rate, unsigned int channels, sample } } - _dbtp_plugin = (Vamp::Plugin**) malloc (sizeof(Vamp::Plugin*) * channels); for (unsigned int c = 0; c < _channels; ++c) { using namespace Vamp::HostExt; PluginLoader* loader (PluginLoader::getInstance ()); - _dbtp_plugin[c] = loader->loadPlugin ("libardourvampplugins:dBTP", sample_rate, PluginLoader::ADAPT_ALL_SAFE); - assert (_dbtp_plugin[c]); - _dbtp_plugin[c]->reset (); - if (!_dbtp_plugin[c]->initialise (1, _bufsize, _bufsize)) { - delete _dbtp_plugin[c]; - _dbtp_plugin[c] = 0; + Vamp::Plugin* dbtp_plugin = loader->loadPlugin ("libardourvampplugins:dBTP", sample_rate, PluginLoader::ADAPT_ALL_SAFE); + dbtp_plugin->reset (); + if (!dbtp_plugin->initialise (1, _bufsize, _bufsize)) { + delete dbtp_plugin; + } else { + _dbtp_plugins.push_back (dbtp_plugin); } } @@ -67,10 +65,10 @@ LoudnessReader::LoudnessReader (float sample_rate, unsigned int channels, sample LoudnessReader::~LoudnessReader () { delete _ebur_plugin; - for (unsigned int c = 0; c < _channels; ++c) { - delete _dbtp_plugin[c]; + while (!_dbtp_plugins.empty()) { + delete _dbtp_plugins.back(); + _dbtp_plugins.pop_back(); } - free (_dbtp_plugin); free (_bufs[0]); free (_bufs[1]); } @@ -82,10 +80,8 @@ LoudnessReader::reset () _ebur_plugin->reset (); } - for (unsigned int c = 0; c < _channels; ++c) { - if (_dbtp_plugin[c]) { - _dbtp_plugin[c]->reset (); - } + for (std::vector<Vamp::Plugin*>::iterator it = _dbtp_plugins.begin (); it != _dbtp_plugins.end(); ++it) { + (*it)->reset (); } } @@ -115,18 +111,17 @@ LoudnessReader::process (ProcessContext<float> const & ctx) } } _ebur_plugin->process (_bufs, Vamp::RealTime::fromSeconds ((double) _pos / _sample_rate)); - if (_dbtp_plugin[0]) { - _dbtp_plugin[0]->process (&_bufs[0], Vamp::RealTime::fromSeconds ((double) _pos / _sample_rate)); + + if (_dbtp_plugins.size() > 0) { + _dbtp_plugins.at(0)->process (&_bufs[0], Vamp::RealTime::fromSeconds ((double) _pos / _sample_rate)); } - if (_channels == 2 && _dbtp_plugin[1]) { - _dbtp_plugin[0]->process (&_bufs[1], Vamp::RealTime::fromSeconds ((double) _pos / _sample_rate)); + /* combined dBTP for EBU-R128 */ + if (_channels == 2 && _dbtp_plugins.size() == 2) { + _dbtp_plugins.at(0)->process (&_bufs[1], Vamp::RealTime::fromSeconds ((double) _pos / _sample_rate)); } } - for (unsigned int c = processed_channels; c < _channels; ++c) { - if (!_dbtp_plugin[c]) { - continue; - } + for (unsigned int c = processed_channels; c < _channels, c < _dbtp_plugins.size (); ++c) { samplecnt_t s; float const * const d = ctx.data (); for (s = 0; s < n_samples; ++s) { @@ -135,7 +130,7 @@ LoudnessReader::process (ProcessContext<float> const & ctx) for (; s < _bufsize; ++s) { _bufs[0][s] = 0.f; } - _dbtp_plugin[c]->process (_bufs, Vamp::RealTime::fromSeconds ((double) _pos / _sample_rate)); + _dbtp_plugins.at(c)->process (_bufs, Vamp::RealTime::fromSeconds ((double) _pos / _sample_rate)); } _pos += n_samples; @@ -159,14 +154,12 @@ LoudnessReader::get_normalize_gain (float target_lufs, float target_dbtp) } } - for (unsigned int c = 0; c < _channels; ++c) { - if (_dbtp_plugin[c]) { - Vamp::Plugin::FeatureSet features = _dbtp_plugin[c]->getRemainingFeatures (); - if (!features.empty () && features.size () == 2) { - const float dbtp = features[0][0].values[0]; - dBTP = std::max (dBTP, dbtp); - ++have_dbtp; - } + for (unsigned int c = 0; c < _channels, c < _dbtp_plugins.size(); ++c) { + Vamp::Plugin::FeatureSet features = _dbtp_plugins.at(c)->getRemainingFeatures (); + if (!features.empty () && features.size () == 2) { + const float dbtp = features[0][0].values[0]; + dBTP = std::max (dBTP, dbtp); + ++have_dbtp; } } |