Don't crash with invalid QModelIndex or when QTreeWidgetItem is NULL in mimeData()

Change-Id: I0a9abaa05cf136eadf222d3e7d102930719b84ff
Reviewed-by: David Faure <david.faure@kdab.com>
This commit is contained in:
Maks Naumov 2015-01-24 21:26:06 +02:00
parent b3fc5e1ea3
commit b444769840
4 changed files with 41 additions and 13 deletions

View File

@ -2942,9 +2942,13 @@ QMimeData *QStandardItemModel::mimeData(const QModelIndexList &indexes) const
itemsSet.reserve(indexes.count());
stack.reserve(indexes.count());
for (int i = 0; i < indexes.count(); ++i) {
QStandardItem *item = itemFromIndex(indexes.at(i));
itemsSet << item;
stack.push(item);
if (QStandardItem *item = itemFromIndex(indexes.at(i))) {
itemsSet << item;
stack.push(item);
} else {
qWarning() << "QStandardItemModel::mimeData: No item associated with invalid index";
return 0;
}
}
//remove duplicates childrens
@ -2978,16 +2982,11 @@ QMimeData *QStandardItemModel::mimeData(const QModelIndexList &indexes) const
//stream everything recursively
while (!stack.isEmpty()) {
QStandardItem *item = stack.pop();
if(itemsSet.contains(item)) { //if the item is selection 'top-level', strem its position
if (itemsSet.contains(item)) //if the item is selection 'top-level', stream its position
stream << item->row() << item->column();
}
if(item) {
stream << *item << item->columnCount() << item->d_ptr->children.count();
stack += item->d_ptr->children;
} else {
QStandardItem dummy;
stream << dummy << 0 << 0;
}
stream << *item << item->columnCount() << item->d_ptr->children.count();
stack += item->d_ptr->children;
}
data->setData(format, encoded);

View File

@ -3273,8 +3273,18 @@ QMimeData *QTreeWidget::mimeData(const QList<QTreeWidgetItem*> items) const
QList<QModelIndex> indexes;
for (int i = 0; i < items.count(); ++i) {
QTreeWidgetItem *item = items.at(i);
if (!item) {
qWarning() << "QTreeWidget::mimeData: Null-item passed";
return 0;
}
for (int c = 0; c < item->values.count(); ++c) {
indexes << indexFromItem(item, c);
const QModelIndex index = indexFromItem(item, c);
if (!index.isValid()) {
qWarning() << "QTreeWidget::mimeData: No index associated with item :" << item;
return 0;
}
indexes << index;
}
}
return d->model->QAbstractItemModel::mimeData(indexes);

View File

@ -129,6 +129,7 @@ private slots:
void removeRowsAndColumns();
void itemRoleNames();
void getMimeDataWithInvalidModelIndex();
private:
QAbstractItemModel *m_model;
@ -1671,6 +1672,13 @@ void tst_QStandardItemModel::itemRoleNames()
VERIFY_MODEL
}
void tst_QStandardItemModel::getMimeDataWithInvalidModelIndex()
{
QStandardItemModel model;
QTest::ignoreMessage(QtWarningMsg, "QStandardItemModel::mimeData: No item associated with invalid index");
QMimeData *data = model.mimeData(QModelIndexList() << QModelIndex());
QVERIFY(!data);
}
QTEST_MAIN(tst_QStandardItemModel)
#include "tst_qstandarditemmodel.moc"

View File

@ -49,6 +49,9 @@ class CustomTreeWidget : public QTreeWidget
public:
QModelIndex indexFromItem(QTreeWidgetItem *item, int column = 0) const
{ return QTreeWidget::indexFromItem(item, column); }
QMimeData * mimeData(const QList<QTreeWidgetItem*> items) const
{ return QTreeWidget::mimeData(items); }
};
class tst_QTreeWidget : public QObject
@ -157,6 +160,7 @@ private slots:
void setChildIndicatorPolicy();
void task20345_sortChildren();
void getMimeDataWithInvalidItem();
public slots:
void itemSelectionChanged();
@ -3368,6 +3372,13 @@ void tst_QTreeWidget::task20345_sortChildren()
QVERIFY(1);
}
void tst_QTreeWidget::getMimeDataWithInvalidItem()
{
CustomTreeWidget w;
QTest::ignoreMessage(QtWarningMsg, "QTreeWidget::mimeData: Null-item passed");
QMimeData *md = w.mimeData(QList<QTreeWidgetItem*>() << Q_NULLPTR);
QVERIFY(!md);
}
QTEST_MAIN(tst_QTreeWidget)
#include "tst_qtreewidget.moc"