From 8ddb278e8a457c72a9211953e632a0aaec9bd4d5 Mon Sep 17 00:00:00 2001 From: Bart De Vries Date: Thu, 27 Jul 2023 13:33:41 +0200 Subject: [PATCH] Fix and prevent nullptr dereferences in AudioManager This would happen when the current track is changed to "no track". Signals from the backend KMediaSession would still trigger duration() calls which would try to dereference the pointer to the current track entry. --- src/audiomanager.cpp | 22 +++++++++++++++------- 1 file changed, 15 insertions(+), 7 deletions(-) diff --git a/src/audiomanager.cpp b/src/audiomanager.cpp index 581be57a..04867a5f 100644 --- a/src/audiomanager.cpp +++ b/src/audiomanager.cpp @@ -390,9 +390,14 @@ void AudioManager::play() if (isStreaming()) { if (!NetworkConnectionManager::instance().streamingAllowed()) { qCDebug(kastsAudio) << "Refusing to play: no connection or streaming on metered connection not allowed"; + QString feedUrl, entryId; + if (d->m_entry) { + feedUrl = d->m_entry->feed()->url(); + entryId = d->m_entry->id(); + } Q_EMIT logError(Error::Type::MeteredStreamingNotAllowed, - d->m_entry->feed()->url(), - d->m_entry->id(), + feedUrl, + entryId, 0, i18n("No connection or streaming on metered connection not allowed"), QString()); @@ -484,7 +489,7 @@ bool AudioManager::canGoNext() const // check if there is a next track if (index < DataManager::instance().queue().count() - 1) { Entry *next_entry = DataManager::instance().getEntry(DataManager::instance().queue()[index + 1]); - if (next_entry->enclosure()) { + if (next_entry && next_entry->enclosure()) { qCDebug(kastsAudio) << "Enclosure status" << next_entry->enclosure()->path() << next_entry->enclosure()->status(); if (next_entry->enclosure()->status() == Enclosure::Downloaded) { return true; @@ -543,9 +548,12 @@ void AudioManager::playerDurationChanged(qint64 duration) qCDebug(kastsAudio) << "AudioManager::playerDurationChanged" << duration; // Check if duration mentioned in enclosure corresponds to real duration - if (duration > 0 && (duration / 1000) != d->m_entry->enclosure()->duration()) { - qCDebug(kastsAudio) << "Correcting duration of" << d->m_entry->id() << "to" << duration / 1000 << "(was" << d->m_entry->enclosure()->duration() << ")"; - d->m_entry->enclosure()->setDuration(duration / 1000); + if (d->m_entry && d->m_entry->enclosure()) { + if (duration > 0 && (duration / 1000) != d->m_entry->enclosure()->duration()) { + qCDebug(kastsAudio) << "Correcting duration of" << d->m_entry->id() << "to" << duration / 1000 << "(was" << d->m_entry->enclosure()->duration() + << ")"; + d->m_entry->enclosure()->setDuration(duration / 1000); + } } qint64 correctedDuration = duration; @@ -580,7 +588,7 @@ void AudioManager::savePlayPosition() checkForPendingSeek(); if (!d->m_lockPositionSaving) { - if (d->m_entry) { + if (d->m_entry && d->m_entry->enclosure()) { if (d->m_entry->enclosure()) { d->m_entry->enclosure()->setPlayPosition(position()); }