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.
This commit is contained in:
Robin Gareus
2019-08-12 15:02:24 +02:00
parent b48a3a6abb
commit 98c65406e3
3 changed files with 33 additions and 40 deletions

View File

@@ -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;

View File

@@ -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];

View File

@@ -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;
}
}