goldendict-ng/audioplayerfactory.cc
Igor Kushnir 19ab09b754 Do not pass null to qobject_cast()
qobject_cast() implementations in Qt4 and Qt5 until version 5.4
unconditionally dereference the argument to access a static member.
This is undefined behavior. When Goldendict is compiled with GCC's
-fsanitize=undefined option in Release mode and launched with
<useInternalPlayer>1</useInternalPlayer> in config, the application
crashes right away with the following message:
runtime error: member call on null pointer of type 'struct AudioPlayer'
2018-04-16 18:12:41 +03:00

85 lines
2.7 KiB
C++

/* This file is (c) 2018 Igor Kushnir <igorkuo@gmail.com>
* Part of GoldenDict. Licensed under GPLv3 or later, see the LICENSE file */
#include <QScopedPointer>
#include <QObject>
#include "audioplayerfactory.hh"
#include "ffmpegaudioplayer.hh"
#include "multimediaaudioplayer.hh"
#include "externalaudioplayer.hh"
#include "gddebug.hh"
AudioPlayerFactory::AudioPlayerFactory( Config::Preferences const & p ) :
useInternalPlayer( p.useInternalPlayer ),
internalPlayerBackend( p.internalPlayerBackend ),
audioPlaybackProgram( p.audioPlaybackProgram )
{
reset();
}
void AudioPlayerFactory::setPreferences( Config::Preferences const & p )
{
if( p.useInternalPlayer != useInternalPlayer )
{
useInternalPlayer = p.useInternalPlayer;
internalPlayerBackend = p.internalPlayerBackend;
audioPlaybackProgram = p.audioPlaybackProgram;
reset();
}
else
if( useInternalPlayer && p.internalPlayerBackend != internalPlayerBackend )
{
internalPlayerBackend = p.internalPlayerBackend;
reset();
}
else
if( !useInternalPlayer && p.audioPlaybackProgram != audioPlaybackProgram )
{
audioPlaybackProgram = p.audioPlaybackProgram;
ExternalAudioPlayer * const externalPlayer =
qobject_cast< ExternalAudioPlayer * >( playerPtr.data() );
if( externalPlayer )
setAudioPlaybackProgram( *externalPlayer );
else
gdWarning( "External player was expected, but it does not exist.\n" );
}
}
void AudioPlayerFactory::reset()
{
if( useInternalPlayer )
{
// qobject_cast checks below account for the case when an unsupported backend
// is stored in config. After this backend is replaced with the default one
// upon preferences saving, the code below does not reset playerPtr with
// another object of the same type.
#ifdef MAKE_FFMPEG_PLAYER
Q_ASSERT( Config::InternalPlayerBackend::defaultBackend().isFfmpeg()
&& "Adjust the code below after changing the default backend." );
if( !internalPlayerBackend.isQtmultimedia() )
{
if( !playerPtr || !qobject_cast< Ffmpeg::AudioPlayer * >( playerPtr.data() ) )
playerPtr.reset( new Ffmpeg::AudioPlayer );
return;
}
#endif
#ifdef MAKE_QTMULTIMEDIA_PLAYER
if( !playerPtr || !qobject_cast< MultimediaAudioPlayer * >( playerPtr.data() ) )
playerPtr.reset( new MultimediaAudioPlayer );
return;
#endif
}
QScopedPointer< ExternalAudioPlayer > externalPlayer( new ExternalAudioPlayer );
setAudioPlaybackProgram( *externalPlayer );
playerPtr.reset( externalPlayer.take() );
}
void AudioPlayerFactory::setAudioPlaybackProgram( ExternalAudioPlayer & externalPlayer )
{
externalPlayer.setPlayerCommandLine( audioPlaybackProgram.trimmed() );
}