From dc97edc080a1af1f7d27fbc83d76b462dc95c388 Mon Sep 17 00:00:00 2001 From: Oskar Wallgren Date: Wed, 25 Jan 2017 03:33:07 +0100 Subject: [PATCH 1/3] Prevent crash on loading too large or long sample --- src/core/SampleBuffer.cpp | 57 +++++++++++++++++++++++++++------------ 1 file changed, 40 insertions(+), 17 deletions(-) diff --git a/src/core/SampleBuffer.cpp b/src/core/SampleBuffer.cpp index c5aeee0417e..f720f4fbbcb 100644 --- a/src/core/SampleBuffer.cpp +++ b/src/core/SampleBuffer.cpp @@ -200,15 +200,39 @@ void SampleBuffer::update( bool _keep_settings ) sample_rate_t samplerate = Engine::mixer()->baseSampleRate(); m_frames = 0; + bool fileLoadError = false; const QFileInfo fileInfo( file ); if( fileInfo.size() > 100*1024*1024 ) { - qWarning( "refusing to load sample files bigger " - "than 100 MB" ); + fileLoadError = true; } else { + SNDFILE * snd_file; + SF_INFO sf_info; + sf_info.format = 0; + if( ( snd_file = sf_open( f, SFM_READ, &sf_info ) ) != NULL ) + { + f_cnt_t frames = sf_info.frames; + int rate = sf_info.samplerate; + if( frames / rate > 60 * 60 ) // 60 minutes + { + fileLoadError = true; + } + sf_close( snd_file ); + } + } + if( fileLoadError ) + { + QMessageBox::information( NULL, + "Fail to open file", + "Audio files are limited to 100 MB " + "in size and 1 hour of playing time", + QMessageBox::Ok ); + } + else + { #ifdef LMMS_HAVE_OGGVORBIS // workaround for a bug in libsndfile or our libsndfile decoder // causing some OGG files to be distorted -> try with OGG Vorbis @@ -237,22 +261,21 @@ void SampleBuffer::update( bool _keep_settings ) } delete[] f; + } - if ( m_frames == 0 ) // if still no frames, bail - { - // sample couldn't be decoded, create buffer containing - // one sample-frame - m_data = MM_ALLOC( sampleFrame, 1 ); - memset( m_data, 0, sizeof( *m_data ) ); - m_frames = 1; - m_loopStartFrame = m_startFrame = 0; - m_loopEndFrame = m_endFrame = 1; - } - else // otherwise normalize sample rate - { - normalizeSampleRate( samplerate, _keep_settings ); - } - + if ( m_frames == 0 || fileLoadError ) // if still no frames, bail + { + // sample couldn't be decoded, create buffer containing + // one sample-frame + m_data = MM_ALLOC( sampleFrame, 1 ); + memset( m_data, 0, sizeof( *m_data ) ); + m_frames = 1; + m_loopStartFrame = m_startFrame = 0; + m_loopEndFrame = m_endFrame = 1; + } + else // otherwise normalize sample rate + { + normalizeSampleRate( samplerate, _keep_settings ); } } else From c3b8f662cbc10d8ed52029480daab4d7516f727e Mon Sep 17 00:00:00 2001 From: Oskar Wallgren Date: Mon, 30 Jan 2017 12:24:47 +0100 Subject: [PATCH 2/3] Move message box to the end --- src/core/SampleBuffer.cpp | 21 +++++++++++---------- 1 file changed, 11 insertions(+), 10 deletions(-) diff --git a/src/core/SampleBuffer.cpp b/src/core/SampleBuffer.cpp index f720f4fbbcb..6d6264d6742 100644 --- a/src/core/SampleBuffer.cpp +++ b/src/core/SampleBuffer.cpp @@ -173,6 +173,7 @@ void SampleBuffer::update( bool _keep_settings ) MM_FREE( m_data ); } + bool fileLoadError = false; if( m_audioFile.isEmpty() && m_origData != NULL && m_origFrames > 0 ) { // TODO: reverse- and amplification-property is not covered @@ -200,7 +201,6 @@ void SampleBuffer::update( bool _keep_settings ) sample_rate_t samplerate = Engine::mixer()->baseSampleRate(); m_frames = 0; - bool fileLoadError = false; const QFileInfo fileInfo( file ); if( fileInfo.size() > 100*1024*1024 ) { @@ -223,15 +223,7 @@ void SampleBuffer::update( bool _keep_settings ) } } - if( fileLoadError ) - { - QMessageBox::information( NULL, - "Fail to open file", - "Audio files are limited to 100 MB " - "in size and 1 hour of playing time", - QMessageBox::Ok ); - } - else + if( ! fileLoadError ) { #ifdef LMMS_HAVE_OGGVORBIS // workaround for a bug in libsndfile or our libsndfile decoder @@ -296,6 +288,15 @@ void SampleBuffer::update( bool _keep_settings ) } emit sampleUpdated(); + + if( fileLoadError ) + { + QMessageBox::information( NULL, + "Fail to open file", + "Audio files are limited to 100 MB " + "in size and 1 hour of playing time", + QMessageBox::Ok ); + } } From fd49f550c9fbe621addb24466b56d4024da89582 Mon Sep 17 00:00:00 2001 From: Oskar Wallgren Date: Thu, 9 Feb 2017 05:48:10 +0100 Subject: [PATCH 3/3] Fix export from command line with large files --- src/core/SampleBuffer.cpp | 20 ++++++++++++++------ 1 file changed, 14 insertions(+), 6 deletions(-) diff --git a/src/core/SampleBuffer.cpp b/src/core/SampleBuffer.cpp index 6d6264d6742..4c1738db532 100644 --- a/src/core/SampleBuffer.cpp +++ b/src/core/SampleBuffer.cpp @@ -22,7 +22,6 @@ * */ - #include "SampleBuffer.h" @@ -58,6 +57,7 @@ #include "DrumSynth.h" #include "endian_handling.h" #include "Engine.h" +#include "GuiApplication.h" #include "interpolation.h" #include "Mixer.h" #include "templates.h" @@ -223,7 +223,7 @@ void SampleBuffer::update( bool _keep_settings ) } } - if( ! fileLoadError ) + if( !fileLoadError ) { #ifdef LMMS_HAVE_OGGVORBIS // workaround for a bug in libsndfile or our libsndfile decoder @@ -291,11 +291,19 @@ void SampleBuffer::update( bool _keep_settings ) if( fileLoadError ) { - QMessageBox::information( NULL, - "Fail to open file", - "Audio files are limited to 100 MB " - "in size and 1 hour of playing time", + QString message = "Audio files are limited to 100 MB " + "in size and 1 hour of playing time"; + if( gui ) + { + QMessageBox::information( NULL, + "Fail to open file", message, QMessageBox::Ok ); + } + else + { + fprintf( stderr, "%s\n", message.toUtf8().constData() ); + exit( EXIT_FAILURE ); + } } }