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.
This commit is contained in:
Bart De Vries 2023-07-27 13:33:41 +02:00
parent 95590dd4e5
commit 8ddb278e8a
1 changed files with 15 additions and 7 deletions

View File

@ -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());
}