From 38238807b85cf9d26e45b00a064b9a14ccdc1438 Mon Sep 17 00:00:00 2001 From: Daniel Weindl Date: Sun, 20 Oct 2024 09:15:23 +0200 Subject: [PATCH] Fix dangling pointer in SolverTest.SettersGettersWithSetup (#2556) Fixes a dangling pointer issue in SolverTest.SettersGettersWithSetup. The SUNContext was destroyed before the associated AmiVectors which resulted in invalid reads. --- .github/workflows/test_valgrind.yml | 1 + scripts/run-valgrind-cpp.sh | 2 +- tests/cpp/unittests/testMisc.cpp | 5 ++--- 3 files changed, 4 insertions(+), 4 deletions(-) diff --git a/.github/workflows/test_valgrind.yml b/.github/workflows/test_valgrind.yml index b3f893647f..9eebbf18f8 100644 --- a/.github/workflows/test_valgrind.yml +++ b/.github/workflows/test_valgrind.yml @@ -4,6 +4,7 @@ on: branches: - master - update_valgrind_supp + - fix_valgrind_tests pull_request: branches: - master diff --git a/scripts/run-valgrind-cpp.sh b/scripts/run-valgrind-cpp.sh index fd08cbda54..a96b1d90e6 100755 --- a/scripts/run-valgrind-cpp.sh +++ b/scripts/run-valgrind-cpp.sh @@ -10,4 +10,4 @@ set -eou pipefail # run tests cd "${AMICI_PATH}/build/" VALGRIND_OPTS="--leak-check=full --error-exitcode=1 --trace-children=yes --show-leak-kinds=definite" -valgrind ${VALGRIND_OPTS} ctest +CTEST_OUTPUT_ON_FAILURE=1 valgrind ${VALGRIND_OPTS} ctest diff --git a/tests/cpp/unittests/testMisc.cpp b/tests/cpp/unittests/testMisc.cpp index b06880072f..0cc7a017dc 100644 --- a/tests/cpp/unittests/testMisc.cpp +++ b/tests/cpp/unittests/testMisc.cpp @@ -360,10 +360,9 @@ TEST_F(SolverTest, SettersGettersWithSetup) ASSERT_EQ(static_cast(solver.getSensitivityMethod()), static_cast(sensi_meth)); - sundials::Context sunctx; auto rdata = std::make_unique(solver, testModel); - AmiVector x(nx, sunctx), dx(nx, sunctx); - AmiVectorArray sx(nx, 1, sunctx), sdx(nx, 1, sunctx); + AmiVector x(nx, solver.getSunContext()), dx(nx, solver.getSunContext()); + AmiVectorArray sx(nx, 1, solver.getSunContext()), sdx(nx, 1, solver.getSunContext()); testModel.setInitialStates(std::vector{ 0 });