mirror of
https://github.com/OPM/ResInsight.git
synced 2026-11-02 09:00:14 -06:00
#14453 Guard against invalid model index in table view
Dropping on an empty area of a table view calls dropMimeData with an invalid parent index, where row and column are -1. PdmUiTableViewQModel::getField() and pdmObjectForRow() used these values to index the child array field, reading out of bounds and returning a garbage object pointer.
This commit is contained in:
1 parent
fe0e691e6c
commit
656944c6fc
3 files changed
+67
No files matched your search
@@ -17,6 +17,7 @@ set(PROJECT_FILES
|
||||
cafUserInterface_UnitTests.cpp
|
||||
cafPdmUiTreeViewModelTest.cpp
|
||||
cafPdmUiTreeSelectionQModelTest.cpp
|
||||
cafPdmUiTableViewQModelTest.cpp
|
||||
cafPdmUiNumberFormatTest.cpp
|
||||
cafPdmUiCheckBoxAndTextEditorTest.cpp
|
||||
cafPdmUiPropertyViewDialogTest.cpp
|
||||
|
||||
+61
@@ -0,0 +1,61 @@
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
|
||||
#include "cafPdmChildArrayField.h"
|
||||
#include "cafPdmField.h"
|
||||
#include "cafPdmObject.h"
|
||||
#include "cafPdmUiTableViewQModel.h"
|
||||
|
||||
#include <QModelIndex>
|
||||
|
||||
using namespace caf;
|
||||
|
||||
class TableRowObject : public caf::PdmObject
|
||||
{
|
||||
CAF_PDM_HEADER_INIT;
|
||||
|
||||
public:
|
||||
TableRowObject()
|
||||
{
|
||||
CAF_PDM_InitObject( "TableRowObject" );
|
||||
CAF_PDM_InitField( &m_value, "Value", 1.0, "Value" );
|
||||
}
|
||||
|
||||
caf::PdmField<double> m_value;
|
||||
};
|
||||
CAF_PDM_SOURCE_INIT( TableRowObject, "TableRowObject" );
|
||||
|
||||
class TableContainerObject : public caf::PdmObject
|
||||
{
|
||||
CAF_PDM_HEADER_INIT;
|
||||
|
||||
public:
|
||||
TableContainerObject()
|
||||
{
|
||||
CAF_PDM_InitObject( "TableContainerObject" );
|
||||
CAF_PDM_InitFieldNoDefault( &m_rows, "Rows", "Rows" );
|
||||
}
|
||||
|
||||
caf::PdmChildArrayField<TableRowObject*> m_rows;
|
||||
};
|
||||
CAF_PDM_SOURCE_INIT( TableContainerObject, "TableContainerObject" );
|
||||
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
/// An invalid model index has row and column set to -1, and must not be used to index the child array
|
||||
//--------------------------------------------------------------------------------------------------
|
||||
TEST( PdmUiTableViewQModelTest, InvalidModelIndex )
|
||||
{
|
||||
TableContainerObject container;
|
||||
container.m_rows.push_back( new TableRowObject );
|
||||
|
||||
PdmUiTableViewQModel model( nullptr );
|
||||
model.setArrayFieldAndBuildEditors( &container.m_rows, "" );
|
||||
|
||||
EXPECT_EQ( 1, model.rowCount() );
|
||||
|
||||
QModelIndex invalidIndex;
|
||||
EXPECT_FALSE( invalidIndex.isValid() );
|
||||
|
||||
EXPECT_EQ( nullptr, model.getField( invalidIndex ) );
|
||||
EXPECT_EQ( nullptr, model.pdmObjectForRow( invalidIndex.row() ) );
|
||||
}
|
||||
Reference in new issue
Block a user