From 7b9c01ec73c0edf8c637ca4ebe58ac80853fdeff Mon Sep 17 00:00:00 2001 From: theverygaming Date: Sun, 6 Aug 2023 01:46:54 +0200 Subject: [PATCH 1/4] fixed buffer overflow in file source --- source_modules/file_source/src/main.cpp | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/source_modules/file_source/src/main.cpp b/source_modules/file_source/src/main.cpp index de827e46..dbd6d710 100644 --- a/source_modules/file_source/src/main.cpp +++ b/source_modules/file_source/src/main.cpp @@ -9,6 +9,15 @@ #include #include #include +#include + +// TODO: figure out where exactly these macros are from (only happens on windows so probably from Windows.h somewhere) +#ifdef min +#undef min +#endif +#ifdef max +#undef max +#endif #define CONCAT(a, b) ((std::string(a) + b).c_str()) @@ -121,7 +130,7 @@ private: } try { _this->reader = new WavReader(_this->fileSelect.path); - _this->sampleRate = _this->reader->getSampleRate(); + _this->sampleRate = std::max(_this->reader->getSampleRate(), (uint32_t)1); core::setInputSampleRate(_this->sampleRate); std::string filename = std::filesystem::path(_this->fileSelect.path).filename().string(); _this->centerFreq = _this->getFrequency(filename); @@ -144,8 +153,8 @@ private: static void worker(void* ctx) { FileSourceModule* _this = (FileSourceModule*)ctx; - double sampleRate = _this->reader->getSampleRate(); - int blockSize = sampleRate / 200.0f; + double sampleRate = std::max(_this->reader->getSampleRate(), (uint32_t)1); + int blockSize = std::min((int)(sampleRate / 200.0f), (int)STREAM_BUFFER_SIZE); int16_t* inBuf = new int16_t[blockSize * 2]; while (true) { @@ -159,8 +168,8 @@ private: static void floatWorker(void* ctx) { FileSourceModule* _this = (FileSourceModule*)ctx; - double sampleRate = _this->reader->getSampleRate(); - int blockSize = sampleRate / 200.0f; + double sampleRate = std::max(_this->reader->getSampleRate(), (uint32_t)1); + int blockSize = std::min((int)(sampleRate / 200.0f), (int)STREAM_BUFFER_SIZE); dsp::complex_t* inBuf = new dsp::complex_t[blockSize]; while (true) { @@ -214,4 +223,4 @@ MOD_EXPORT void _DELETE_INSTANCE_(void* instance) { MOD_EXPORT void _END_() { config.disableAutoSave(); config.save(); -} \ No newline at end of file +} From 6b31134af293bbad52f103f80d6c0ffc5029f10a Mon Sep 17 00:00:00 2001 From: theverygaming Date: Sun, 27 Aug 2023 13:28:21 +0200 Subject: [PATCH 2/4] do not play file when samplerate == 0 --- source_modules/file_source/src/main.cpp | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/source_modules/file_source/src/main.cpp b/source_modules/file_source/src/main.cpp index dbd6d710..a10f3c58 100644 --- a/source_modules/file_source/src/main.cpp +++ b/source_modules/file_source/src/main.cpp @@ -10,6 +10,7 @@ #include #include #include +#include // TODO: figure out where exactly these macros are from (only happens on windows so probably from Windows.h somewhere) #ifdef min @@ -130,7 +131,13 @@ private: } try { _this->reader = new WavReader(_this->fileSelect.path); - _this->sampleRate = std::max(_this->reader->getSampleRate(), (uint32_t)1); + if (_this->reader->getSampleRate() == 0) { + _this->reader->close(); + delete _this->reader; + _this->reader = NULL; + throw std::runtime_error("Sample rate may not be zero"); + } + _this->sampleRate = _this->reader->getSampleRate(); core::setInputSampleRate(_this->sampleRate); std::string filename = std::filesystem::path(_this->fileSelect.path).filename().string(); _this->centerFreq = _this->getFrequency(filename); @@ -139,7 +146,7 @@ private: //gui::freqSelect.maxFreq = _this->centerFreq + (_this->sampleRate/2); //gui::freqSelect.limitFreq = true; } - catch (std::exception e) { + catch (std::exception& e) { flog::error("Error: {0}", e.what()); } config.acquire(); From 0e50ee0e67a04836a5293d00b6143322726d04b2 Mon Sep 17 00:00:00 2001 From: AlexandreRouma Date: Wed, 13 Sep 2023 02:51:44 +0200 Subject: [PATCH 3/4] Update main.cpp --- source_modules/file_source/src/main.cpp | 8 -------- 1 file changed, 8 deletions(-) diff --git a/source_modules/file_source/src/main.cpp b/source_modules/file_source/src/main.cpp index a10f3c58..0fc4fa85 100644 --- a/source_modules/file_source/src/main.cpp +++ b/source_modules/file_source/src/main.cpp @@ -12,14 +12,6 @@ #include #include -// TODO: figure out where exactly these macros are from (only happens on windows so probably from Windows.h somewhere) -#ifdef min -#undef min -#endif -#ifdef max -#undef max -#endif - #define CONCAT(a, b) ((std::string(a) + b).c_str()) SDRPP_MOD_INFO{ From a824c8384802eeb5e43017bf1c42ac7310d1c8b0 Mon Sep 17 00:00:00 2001 From: AlexandreRouma Date: Wed, 13 Sep 2023 03:16:12 +0200 Subject: [PATCH 4/4] Update main.cpp --- source_modules/file_source/src/main.cpp | 1 + 1 file changed, 1 insertion(+) diff --git a/source_modules/file_source/src/main.cpp b/source_modules/file_source/src/main.cpp index 0fc4fa85..0628a412 100644 --- a/source_modules/file_source/src/main.cpp +++ b/source_modules/file_source/src/main.cpp @@ -1,3 +1,4 @@ +#define NOMINMAX #include #include #include