Skip to content

Commit 7b9c01e

Browse files
committed
fixed buffer overflow in file source
1 parent f06eccd commit 7b9c01e

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

source_modules/file_source/src/main.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,15 @@
99
#include <filesystem>
1010
#include <regex>
1111
#include <gui/tuner.h>
12+
#include <algorithm>
13+
14+
// TODO: figure out where exactly these macros are from (only happens on windows so probably from Windows.h somewhere)
15+
#ifdef min
16+
#undef min
17+
#endif
18+
#ifdef max
19+
#undef max
20+
#endif
1221

1322
#define CONCAT(a, b) ((std::string(a) + b).c_str())
1423

@@ -121,7 +130,7 @@ class FileSourceModule : public ModuleManager::Instance {
121130
}
122131
try {
123132
_this->reader = new WavReader(_this->fileSelect.path);
124-
_this->sampleRate = _this->reader->getSampleRate();
133+
_this->sampleRate = std::max(_this->reader->getSampleRate(), (uint32_t)1);
125134
core::setInputSampleRate(_this->sampleRate);
126135
std::string filename = std::filesystem::path(_this->fileSelect.path).filename().string();
127136
_this->centerFreq = _this->getFrequency(filename);
@@ -144,8 +153,8 @@ class FileSourceModule : public ModuleManager::Instance {
144153

145154
static void worker(void* ctx) {
146155
FileSourceModule* _this = (FileSourceModule*)ctx;
147-
double sampleRate = _this->reader->getSampleRate();
148-
int blockSize = sampleRate / 200.0f;
156+
double sampleRate = std::max(_this->reader->getSampleRate(), (uint32_t)1);
157+
int blockSize = std::min((int)(sampleRate / 200.0f), (int)STREAM_BUFFER_SIZE);
149158
int16_t* inBuf = new int16_t[blockSize * 2];
150159

151160
while (true) {
@@ -159,8 +168,8 @@ class FileSourceModule : public ModuleManager::Instance {
159168

160169
static void floatWorker(void* ctx) {
161170
FileSourceModule* _this = (FileSourceModule*)ctx;
162-
double sampleRate = _this->reader->getSampleRate();
163-
int blockSize = sampleRate / 200.0f;
171+
double sampleRate = std::max(_this->reader->getSampleRate(), (uint32_t)1);
172+
int blockSize = std::min((int)(sampleRate / 200.0f), (int)STREAM_BUFFER_SIZE);
164173
dsp::complex_t* inBuf = new dsp::complex_t[blockSize];
165174

166175
while (true) {
@@ -214,4 +223,4 @@ MOD_EXPORT void _DELETE_INSTANCE_(void* instance) {
214223
MOD_EXPORT void _END_() {
215224
config.disableAutoSave();
216225
config.save();
217-
}
226+
}

0 commit comments

Comments
 (0)