Skip to content

Commit

Permalink
Rx channel demods: prevent possible reading past audio buffer end - t…
Browse files Browse the repository at this point in the history
…ake 2. For #1661
  • Loading branch information
f4exb committed Apr 25, 2023
1 parent 59e664c commit 3f392d3
Show file tree
Hide file tree
Showing 26 changed files with 33 additions and 37 deletions.
4 changes: 2 additions & 2 deletions plugins/channelrx/demodam/amdemodsink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -203,11 +203,11 @@ void AMDemodSink::processOneSample(Complex &ci)

if (m_audioBufferFill >= m_audioBuffer.size())
{
uint res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], m_audioBufferFill);
std:;size_t res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], std::min(m_audioBufferFill, m_audioBuffer.size()));

if (res != m_audioBufferFill)
{
qDebug("AMDemodSink::processOneSample: %u/%u audio samples written", res, m_audioBufferFill);
qDebug("AMDemodSink::processOneSample: %lu/%lu audio samples written", res, m_audioBufferFill);
m_audioFifo.clear();
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/channelrx/demodam/amdemodsink.h
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ class AMDemodSink : public ChannelSampleSink {

AudioVector m_audioBuffer;
AudioFifo m_audioFifo;
uint32_t m_audioBufferFill;
std::size_t m_audioBufferFill;
QVector<qint16> m_demodBuffer;
int m_demodBufferFill;

Expand Down
4 changes: 2 additions & 2 deletions plugins/channelrx/demodbfm/bfmdemodsink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -225,10 +225,10 @@ void BFMDemodSink::feed(const SampleVector::const_iterator& begin, const SampleV

if (m_audioBufferFill >= m_audioBuffer.size())
{
uint res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], m_audioBufferFill);
std::size_t res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], std::min(m_audioBufferFill, m_audioBuffer.size()));

if(res != m_audioBufferFill) {
qDebug("BFMDemodSink::feed: %u/%u audio samples written", res, m_audioBufferFill);
qDebug("BFMDemodSink::feed: %lu/%lu audio samples written", res, m_audioBufferFill);
}

m_audioBufferFill = 0;
Expand Down
2 changes: 1 addition & 1 deletion plugins/channelrx/demodbfm/bfmdemodsink.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class BFMDemodSink : public ChannelSampleSink {

int m_audioSampleRate;
AudioVector m_audioBuffer;
uint m_audioBufferFill;
std::size_t m_audioBufferFill;
AudioFifo m_audioFifo;
SampleVector m_sampleBuffer;

Expand Down
4 changes: 2 additions & 2 deletions plugins/channelrx/demoddab/dabdemodsink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -446,11 +446,11 @@ void DABDemodSink::processOneAudioSample(Complex &ci)

if (m_audioBufferFill >= m_audioBuffer.size())
{
uint res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], m_audioBufferFill);
std::size_t res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], std::min(m_audioBufferFill, m_audioBuffer.size()));

if (res != m_audioBufferFill)
{
qDebug("DABDemodSink::audio: %u/%u audio samples written", res, m_audioBufferFill);
qDebug("DABDemodSink::audio: %lu/%lu audio samples written", res, m_audioBufferFill);
m_audioFifo.clear();
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/channelrx/demoddab/dabdemodsink.h
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ class DABDemodSink : public ChannelSampleSink {
Real m_audioInterpolatorDistanceRemain;
AudioVector m_audioBuffer;
AudioFifo m_audioFifo;
uint32_t m_audioBufferFill;
std::size_t m_audioBufferFill;

QVector<qint16> m_demodBuffer;
int m_demodBufferFill;
Expand Down
1 change: 0 additions & 1 deletion plugins/channelrx/demoddsd/dsddemodsink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,6 @@ DSDDemodSink::DSDDemodSink() :
m_signalFormat(signalFormatNone)
{
m_audioBuffer.resize(1<<14);
m_audioBufferFill = 0;
m_demodBuffer.resize(1<<12);
m_demodBufferFill = 0;

Expand Down
1 change: 0 additions & 1 deletion plugins/channelrx/demoddsd/dsddemodsink.h
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,6 @@ class DSDDemodSink : public ChannelSampleSink {

SampleVector m_scopeSampleBuffer;
AudioVector m_audioBuffer;
uint m_audioBufferFill;
FixReal *m_sampleBuffer; //!< samples ring buffer
int m_sampleBufferIndex;
int m_scaleFromShort;
Expand Down
4 changes: 2 additions & 2 deletions plugins/channelrx/demodfreedv/freedvdemodsink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -330,10 +330,10 @@ void FreeDVDemodSink::pushSampleToAudio(int16_t sample)

if (m_audioBufferFill >= m_audioBuffer.size())
{
uint res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], m_audioBufferFill);
std::size_t res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], std::min(m_audioBufferFill, m_audioBuffer.size()));

if (res != m_audioBufferFill) {
qDebug("FreeDVDemodSink::pushSampleToAudio: %u/%u samples written", res, m_audioBufferFill);
qDebug("FreeDVDemodSink::pushSampleToAudio: %lu/%lu samples written", res, m_audioBufferFill);
}

m_audioBufferFill = 0;
Expand Down
2 changes: 1 addition & 1 deletion plugins/channelrx/demodfreedv/freedvdemodsink.h
Original file line number Diff line number Diff line change
Expand Up @@ -182,7 +182,7 @@ class FreeDVDemodSink : public ChannelSampleSink {
SampleVector m_sampleBuffer;

AudioVector m_audioBuffer;
uint m_audioBufferFill;
std::size_t m_audioBufferFill;
AudioFifo m_audioFifo;

struct freedv *m_freeDV;
Expand Down
4 changes: 2 additions & 2 deletions plugins/channelrx/demodils/ilsdemodsink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -316,11 +316,11 @@ void ILSDemodSink::processOneAudioSample(Complex &ci)

if (m_audioBufferFill >= m_audioBuffer.size())
{
uint res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], m_audioBufferFill);
std::size_t res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], std::min(m_audioBufferFill, m_audioBuffer.size()));

if (res != m_audioBufferFill)
{
qDebug("ILSDemodSink::processOneAudioSample: %u/%u audio samples written", res, m_audioBufferFill);
qDebug("ILSDemodSink::processOneAudioSample: %lu/%lu audio samples written", res, m_audioBufferFill);
m_audioFifo.clear();
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/channelrx/demodils/ilsdemodsink.h
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ class ILSDemodSink : public ChannelSampleSink {
Real m_audioInterpolatorDistanceRemain;
AudioVector m_audioBuffer;
AudioFifo m_audioFifo;
uint32_t m_audioBufferFill;
std::size_t m_audioBufferFill;

SampleVector m_sampleBuffer;
static const int m_sampleBufferSize = ILSDemodSettings::ILSDEMOD_CHANNEL_SAMPLE_RATE / 20;
Expand Down
4 changes: 2 additions & 2 deletions plugins/channelrx/demodm17/m17demodprocessor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -520,10 +520,10 @@ void M17DemodProcessor::processAudio(const std::array<int16_t, 160>& in)

if (m_audioBufferFill >= m_audioBuffer.size() - 960)
{
uint res = m_audioFifo->write((const quint8*)&m_audioBuffer[0], m_audioBufferFill);
std::size_t res = m_audioFifo->write((const quint8*)&m_audioBuffer[0], std::min(m_audioBufferFill, m_audioBuffer.size()));

if (res != m_audioBufferFill) {
qDebug("M17DemodProcessor::processAudio: %u/%u audio samples written", res, m_audioBufferFill);
qDebug("M17DemodProcessor::processAudio: %lu/%lu audio samples written", res, m_audioBufferFill);
}

m_audioBufferFill = 0;
Expand Down
2 changes: 1 addition & 1 deletion plugins/channelrx/demodm17/m17demodprocessor.h
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ class M17DemodProcessor : public QObject
AudioFifo *m_audioFifo;
bool m_audioMute;
AudioVector m_audioBuffer;
uint m_audioBufferFill;
std::size_t m_audioBufferFill;
float m_volume;
int m_upsampling; //!< upsampling factor
float m_upsamplingFactors[7];
Expand Down
1 change: 0 additions & 1 deletion plugins/channelrx/demodm17/m17demodsink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,6 @@ M17DemodSink::M17DemodSink() :
m_scopeEnabled(true)
{
m_audioBuffer.resize(1<<14);
m_audioBufferFill = 0;
m_demodBuffer.resize(1<<12);
m_demodBufferFill = 0;
m_m17DemodProcessor.setAudioFifo(&m_audioFifo);
Expand Down
1 change: 0 additions & 1 deletion plugins/channelrx/demodm17/m17demodsink.h
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,6 @@ class M17DemodSink : public ChannelSampleSink {

SampleVector m_scopeSampleBuffer;
AudioVector m_audioBuffer;
uint m_audioBufferFill;
FixReal *m_sampleBuffer; //!< samples ring buffer
int m_sampleBufferIndex;
int m_scaleFromShort;
Expand Down
4 changes: 2 additions & 2 deletions plugins/channelrx/demodnfm/nfmdemodsink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -244,11 +244,11 @@ void NFMDemodSink::processOneSample(Complex &ci)

if (m_audioBufferFill >= m_audioBuffer.size())
{
uint res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], m_audioBufferFill);
std::size_t res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], std::min(m_audioBufferFill, m_audioBuffer.size()));

if (res != m_audioBufferFill)
{
qDebug("NFMDemodSink::processOneSample: %u/%u audio samples written m_audioSampleRate: %u m_channelSampleRate: %d",
qDebug("NFMDemodSink::processOneSample: %lu/%lu audio samples written m_audioSampleRate: %u m_channelSampleRate: %d",
res, m_audioBufferFill, m_audioSampleRate, m_channelSampleRate);
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/channelrx/demodnfm/nfmdemodsink.h
Original file line number Diff line number Diff line change
Expand Up @@ -103,7 +103,7 @@ class NFMDemodSink : public ChannelSampleSink {

int m_audioSampleRate;
AudioVector m_audioBuffer;
uint m_audioBufferFill;
std::size_t m_audioBufferFill;
AudioFifo m_audioFifo;
QVector<qint16> m_demodBuffer;
int m_demodBufferFill;
Expand Down
4 changes: 2 additions & 2 deletions plugins/channelrx/demodssb/ssbdemodsink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -241,10 +241,10 @@ void SSBDemodSink::processOneSample(Complex &ci)

if (m_audioBufferFill >= m_audioBuffer.size())
{
uint res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], m_audioBufferFill);
std::size_t res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], std::min(m_audioBufferFill, m_audioBuffer.size()));

if (res != m_audioBufferFill) {
qDebug("SSBDemodSink::processOneSample: %u/%u samples written", res, m_audioBufferFill);
qDebug("SSBDemodSink::processOneSample: %lu/%lu samples written", res, m_audioBufferFill);
}

m_audioBufferFill = 0;
Expand Down
2 changes: 1 addition & 1 deletion plugins/channelrx/demodssb/ssbdemodsink.h
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class SSBDemodSink : public ChannelSampleSink {
SampleVector m_sampleBuffer;

AudioVector m_audioBuffer;
uint m_audioBufferFill;
std::size_t m_audioBufferFill;
AudioFifo m_audioFifo;
quint32 m_audioSampleRate;

Expand Down
4 changes: 2 additions & 2 deletions plugins/channelrx/demodvor/vordemodsink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -149,11 +149,11 @@ void VORDemodSCSink::processOneAudioSample(Complex &ci)

if (m_audioBufferFill >= m_audioBuffer.size())
{
uint res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], m_audioBufferFill);
std::size_t res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], std::min(m_audioBufferFill, m_audioBuffer.size()));

if (res != m_audioBufferFill)
{
qDebug("VORDemodSCSink::processOneAudioSample: %u/%u audio samples written", res, m_audioBufferFill);
qDebug("VORDemodSCSink::processOneAudioSample: %lu/%lu audio samples written", res, m_audioBufferFill);
m_audioFifo.clear();
}

Expand Down
2 changes: 1 addition & 1 deletion plugins/channelrx/demodvor/vordemodsink.h
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ class VORDemodSCSink : public ChannelSampleSink {
Real m_audioInterpolatorDistanceRemain;
AudioVector m_audioBuffer;
AudioFifo m_audioFifo;
uint32_t m_audioBufferFill;
std::size_t m_audioBufferFill;

NCO m_ncoRef;
Lowpass<Complex> m_lowpassRef;
Expand Down
4 changes: 2 additions & 2 deletions plugins/channelrx/demodwfm/wfmdemodsink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,10 +126,10 @@ void WFMDemodSink::feed(const SampleVector::const_iterator& begin, const SampleV

if(m_audioBufferFill >= m_audioBuffer.size())
{
uint res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], m_audioBufferFill);
std::size_t res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], std::min(m_audioBufferFill, m_audioBuffer.size()));

if (res != m_audioBufferFill) {
qDebug("WFMDemodSink::feed: %u/%u audio samples written", res, m_audioBufferFill);
qDebug("WFMDemodSink::feed: %lu/%lu audio samples written", res, m_audioBufferFill);
}

m_audioBufferFill = 0;
Expand Down
2 changes: 1 addition & 1 deletion plugins/channelrx/demodwfm/wfmdemodsink.h
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ class WFMDemodSink : public ChannelSampleSink {
Real m_fmExcursion;

AudioVector m_audioBuffer;
uint m_audioBufferFill;
std::size_t m_audioBufferFill;

AudioFifo m_audioFifo;
PhaseDiscriminators m_phaseDiscri;
Expand Down
4 changes: 2 additions & 2 deletions plugins/channelrx/udpsink/udpsinksink.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -314,10 +314,10 @@ void UDPSinkSink::audioReadyRead()

if (m_audioBufferFill >= m_audioBuffer.size())
{
uint res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], m_audioBufferFill);
std::size_t res = m_audioFifo.write((const quint8*)&m_audioBuffer[0], std::min(m_audioBufferFill, m_sampleBuffer.size()));

if (res != m_audioBufferFill) {
qDebug("UDPSinkSink::audioReadyRead: (stereo) lost %u samples", m_audioBufferFill - res);
qDebug("WFMDemodSink::feed: %lu/%lu audio samples written", res, m_audioBufferFill);
}

m_audioBufferFill = 0;
Expand Down
2 changes: 1 addition & 1 deletion plugins/channelrx/udpsink/udpsinksink.h
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ private slots:
UDPSinkUtil<Sample24> *m_udpBuffer24;

AudioVector m_audioBuffer;
uint m_audioBufferFill;
std::size_t m_audioBufferFill;
AudioFifo m_audioFifo;

BasebandSampleSink* m_spectrum;
Expand Down

0 comments on commit 3f392d3

Please sign in to comment.