Files
ResInsight/ApplicationLibCode/UnitTests/RimViewController-Test.cpp
T
Magne Sjaastad b22a2e3106 Fix crash in RimViewController::masterView when detached from view linker
RimViewController::masterView() dereferenced ownerViewLinker() without a
null check, causing a crash when the controller has been detached from
its RimViewLinker parent (e.g. mid-destruction, after the owning
child-array field has already erased it but before delete runs).

The same unguarded ownerViewLinker() dereference existed in several other
methods: isActive(), isCameraLinked(), isTimeStepLinked(),
isResultColorControlled(), isLegendDefinitionsControlled(),
isPropertyFilterOveridden(), updateOverrides(),
updateDuplicatedPropertyFilters(), updateCameraLink(),
updateTimeStepLink(), updateResultColorsControl(),
updateLegendDefinitions() and applyCellFilterCollectionByUserChoice().
All now store ownerViewLinker() in a local and null-check it before use,
matching the pattern already used in isCellFiltersControlled().
2026-10-05 19:56:10 +02:00

55 lines
2.7 KiB
C++

/////////////////////////////////////////////////////////////////////////////////
//
// Copyright (C) 2026 Equinor ASA
//
// ResInsight is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// ResInsight is distributed in the hope that it will be useful, but WITHOUT ANY
// WARRANTY; without even the implied warranty of MERCHANTABILITY or
// FITNESS FOR A PARTICULAR PURPOSE.
//
// See the GNU General Public License at <http://www.gnu.org/licenses/gpl.html>
// for more details.
//
/////////////////////////////////////////////////////////////////////////////////
#include "gtest/gtest.h"
#include "RimViewController.h"
//--------------------------------------------------------------------------------------------------
/// RimViewController::masterView() used to dereference ownerViewLinker() unchecked. When a view
/// controller has been detached from its RimViewLinker parent (e.g. mid-destruction, after the
/// owning child-array field has already erased it but before `delete` runs), ownerViewLinker()
/// returns null and the call used to crash. See the crash in RicDeleteItemExec::redo() -> ~RimViewController()
/// -> removeOverrides() -> ... -> masterView().
//--------------------------------------------------------------------------------------------------
TEST( RimViewController, MasterViewWithoutOwnerViewLinkerDoesNotCrash )
{
RimViewController controller;
EXPECT_EQ( nullptr, controller.masterView() );
EXPECT_FALSE( controller.isPropertyFilterOveridden() );
}
//--------------------------------------------------------------------------------------------------
/// Several other methods in RimViewController had the same unguarded ownerViewLinker() dereference as
/// masterView() used to have. isActive() and updateOverrides() dereference ownerViewLinker() directly
/// (without going through a checked helper first), so they are the most directly reachable without any
/// extra setup when the controller has been detached from its RimViewLinker parent.
//--------------------------------------------------------------------------------------------------
TEST( RimViewController, IsActiveAndUpdateOverridesWithoutOwnerViewLinkerDoesNotCrash )
{
RimViewController controller;
EXPECT_FALSE( controller.isActive() );
EXPECT_FALSE( controller.isCameraLinked() );
EXPECT_FALSE( controller.isTimeStepLinked() );
EXPECT_FALSE( controller.isResultColorControlled() );
EXPECT_FALSE( controller.isLegendDefinitionsControlled() );
controller.updateOverrides();
controller.applyCellFilterCollectionByUserChoice();
}