From 49925e2a9598f27a526534dc2b4b1bc0bcee639b Mon Sep 17 00:00:00 2001
From: Harald Sitter <sitter@kde.org>
Date: Fri, 15 Nov 2024 13:36:04 +0000
Subject: [PATCH] listitemmenu: guard activePort against null

it's not quite obvious why ports.at(index) would return null, but it's
my best guess for why we have a crash here

CCBUG: 496067


(cherry picked from commit 23c8a3f982d5e0c8380c0870b047b251f67d179e)

Co-authored-by: Harald Sitter <sitter@kde.org>
---
 src/qml/listitemmenu.cpp | 7 ++++---
 1 file changed, 4 insertions(+), 3 deletions(-)

diff --git a/src/qml/listitemmenu.cpp b/src/qml/listitemmenu.cpp
index 5196a3de..9319bd26 100644
--- a/src/qml/listitemmenu.cpp
+++ b/src/qml/listitemmenu.cpp
@@ -333,9 +333,10 @@ QMenu *ListItemMenu::createMenu()
         // Ports
         const auto ports = device->ports();
         bool activePortUnavailable = false;
-        if (device->activePortIndex() != static_cast<quint32>(-1)) {
-            auto *activePort = static_cast<Port *>(ports.at(device->activePortIndex()));
-            activePortUnavailable = activePort->availability() == Port::Unavailable;
+        if (auto index = device->activePortIndex(); index != static_cast<quint32>(-1)) {
+            if (auto activePort = ports.at(index)) {
+                activePortUnavailable = activePort->availability() == Port::Unavailable;
+            }
         }
 
         QMap<int, Port *> availablePorts;
-- 
GitLab

