From cc2e2211d9e2853f03b3b1210974ff34af04aaf9 Mon Sep 17 00:00:00 2001 From: hjk Date: Fri, 22 Nov 2024 10:00:39 +0100 Subject: [PATCH] Utils: Prevent crash when inserting into an empty environment model Change-Id: Id857b21709d12534175a697625b3f5030f05fcd9 Reviewed-by: Christian Kandeler Reviewed-by: Marcus Tillmanns --- src/libs/utils/environmentmodel.cpp | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/libs/utils/environmentmodel.cpp b/src/libs/utils/environmentmodel.cpp index 20efb301ccd..9d5edb684fd 100644 --- a/src/libs/utils/environmentmodel.cpp +++ b/src/libs/utils/environmentmodel.cpp @@ -67,7 +67,8 @@ public: int findInResultInsertPosition(const QString &key) const { const auto compare = [](int compareResult) { return compareResult > 0; }; - return findIndex(m_resultNameValueDictionary, key, compare); + const int pos = findIndex(m_resultNameValueDictionary, key, compare); + return pos >= 0 ? pos : m_resultNameValueDictionary.size(); } int findInResult(const QString &key) const @@ -291,6 +292,7 @@ QModelIndex EnvironmentModel::addVariable(const EnvironmentItem &item) } else { // We add something that is not in the base dictionary // Insert a new line! + QTC_ASSERT(insertPos >= 0, insertPos = d->m_resultNameValueDictionary.size()); beginInsertRows(QModelIndex(), insertPos, insertPos); Q_ASSERT(changePos < 0); d->m_items.append(item);