DocumentModel: Fix possible use of deallocated memory

Setting entry to nullptr inside DocumentModelPrivate::addEntry()
sets it only locally inside this function, so the caller of
addEntry will still hold the originally passed pointer. So, if the
"if (previousEntry)" condition was hold, the caller of addEntry
operates on deleted pointer.

In order to fix it we return the valid pointer instead.

Change-Id: I3e4a5c3532ca942c43ab422a238f0a19ebd41794
Reviewed-by: Eike Ziller <eike.ziller@qt.io>
This commit is contained in:
Jarek Kobus
2022-11-29 21:22:22 +01:00
parent e53a03a74e
commit 10a306b3f4
2 changed files with 5 additions and 6 deletions

View File

@@ -79,7 +79,7 @@ int DocumentModelPrivate::rowCount(const QModelIndex &parent) const
return 0;
}
void DocumentModelPrivate::addEntry(DocumentModel::Entry *entry)
DocumentModel::Entry *DocumentModelPrivate::addEntry(DocumentModel::Entry *entry)
{
const Utils::FilePath filePath = entry->fileName();
@@ -95,9 +95,8 @@ void DocumentModelPrivate::addEntry(DocumentModel::Entry *entry)
this, [this, document = previousEntry->document] { itemChanged(document); });
}
delete entry;
entry = nullptr;
disambiguateDisplayNames(previousEntry);
return;
return nullptr;
}
auto positions = positionEntry(m_entries, entry);
@@ -115,6 +114,7 @@ void DocumentModelPrivate::addEntry(DocumentModel::Entry *entry)
itemChanged(document);
});
endInsertRows();
return entry;
}
bool DocumentModelPrivate::disambiguateDisplayNames(DocumentModel::Entry *entry)
@@ -411,8 +411,7 @@ DocumentModel::Entry *DocumentModelPrivate::addSuspendedDocument(const FilePath
entry->document->setPreferredDisplayName(displayName);
entry->document->setId(id);
entry->isSuspended = true;
d->addEntry(entry);
return entry;
return d->addEntry(entry);
}
DocumentModel::Entry *DocumentModelPrivate::firstSuspendedEntry()

View File

@@ -33,7 +33,7 @@ public:
Qt::DropActions supportedDragActions() const override;
QStringList mimeTypes() const override;
void addEntry(DocumentModel::Entry *entry);
DocumentModel::Entry *addEntry(DocumentModel::Entry *entry);
void removeDocument(int idx);
std::optional<int> indexOfFilePath(const Utils::FilePath &filePath) const;