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'
This commit is contained in:
Igor Kushnir 2018-04-16 16:21:02 +03:00
parent fb71eb4ce1
commit 19ab09b754

View file

@ -60,14 +60,14 @@ void AudioPlayerFactory::reset()
if( !internalPlayerBackend.isQtmultimedia() )
{
if( qobject_cast< Ffmpeg::AudioPlayer * >( playerPtr.data() ) == 0 )
if( !playerPtr || !qobject_cast< Ffmpeg::AudioPlayer * >( playerPtr.data() ) )
playerPtr.reset( new Ffmpeg::AudioPlayer );
return;
}
#endif
#ifdef MAKE_QTMULTIMEDIA_PLAYER
if( qobject_cast< MultimediaAudioPlayer * >( playerPtr.data() ) == 0 )
if( !playerPtr || !qobject_cast< MultimediaAudioPlayer * >( playerPtr.data() ) )
playerPtr.reset( new MultimediaAudioPlayer );
return;
#endif