Utils: Prevent crash when inserting into an empty environment model

Change-Id: Id857b21709d12534175a697625b3f5030f05fcd9
Reviewed-by: Christian Kandeler <christian.kandeler@qt.io>
Reviewed-by: Marcus Tillmanns <marcus.tillmanns@qt.io>
This commit is contained in:
hjk
2024-11-22 10:00:39 +01:00
parent 5d0331c5ca
commit cc2e2211d9

View File

@@ -67,7 +67,8 @@ public:
int findInResultInsertPosition(const QString &key) const int findInResultInsertPosition(const QString &key) const
{ {
const auto compare = [](int compareResult) { return compareResult > 0; }; 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 int findInResult(const QString &key) const
@@ -291,6 +292,7 @@ QModelIndex EnvironmentModel::addVariable(const EnvironmentItem &item)
} else { } else {
// We add something that is not in the base dictionary // We add something that is not in the base dictionary
// Insert a new line! // Insert a new line!
QTC_ASSERT(insertPos >= 0, insertPos = d->m_resultNameValueDictionary.size());
beginInsertRows(QModelIndex(), insertPos, insertPos); beginInsertRows(QModelIndex(), insertPos, insertPos);
Q_ASSERT(changePos < 0); Q_ASSERT(changePos < 0);
d->m_items.append(item); d->m_items.append(item);