QFileDialog: Fix a crash occurring when deleting a file

A crash has been observed on Windows in the use case below:
1) In a non-native QFileDialog, select a file and press "Del" key to
delete it, and a warning message box appears for user to confirm the
deletion.
2) Delete the file in the Windows Explorer.
3) Click "Yes" on the warning message box, a crash happens
In QFileDialog::_q_deleteCurrent(), use QPersistentModelIndex
instead of QModelIndex to ensure that the index is valid to be
deleted. The change is intended to fix .

The patch is to use QPersistentModelIndex instead of QModelIndex
in QFileDialog::_q_deleteCurrent() to ensure that the index is
valid to be deleted.

Change-Id: I8959124dc071f7cf0ab47f954d611211a789978d
Reviewed-by: Friedemann Kleint <Friedemann.Kleint@qt.io>
This commit is contained in:
Dongmei Wang 2017-07-25 13:11:04 -07:00
parent 763b0a68be
commit aca9c93fa0

View File

@ -3417,7 +3417,7 @@ void QFileDialogPrivate::_q_deleteCurrent()
QModelIndexList list = qFileDialogUi->listView->selectionModel()->selectedRows();
for (int i = list.count() - 1; i >= 0; --i) {
QModelIndex index = list.at(i);
QPersistentModelIndex index = list.at(i);
if (index == qFileDialogUi->listView->rootIndex())
continue;
@ -3443,12 +3443,15 @@ void QFileDialogPrivate::_q_deleteCurrent()
QMessageBox::Yes | QMessageBox::No, QMessageBox::No) == QMessageBox::No)
return;
// the event loop has run, we have to validate if the index is valid because the model might have removed it.
if (!index.isValid())
return;
#else
if (!(p & QFile::WriteUser))
return;
#endif // QT_CONFIG(messagebox)
// the event loop has run, we can NOT reuse index because the model might have removed it.
if (isDir) {
if (!removeDirectory(filePath)) {
#if QT_CONFIG(messagebox)