From 761d9cd9a68ffa7210795d99f791db51ae870bc1 Mon Sep 17 00:00:00 2001 From: Samuel Leweke Date: Thu, 26 May 2022 00:31:13 +0200 Subject: [PATCH] Fix binding model tests that use external funcs In binding model tests, the external function array was not created properly. This lead to SIGILL (invalid instructions) on delete[]-ing the array. The offending construct has been replaced by a std::vector. --- test/BindingModelTests.cpp | 22 +++++++++++++++------- test/BindingModelTests.hpp | 12 +++++------- 2 files changed, 20 insertions(+), 14 deletions(-) diff --git a/test/BindingModelTests.cpp b/test/BindingModelTests.cpp index a5e0d8184..f14b8f7f0 100644 --- a/test/BindingModelTests.cpp +++ b/test/BindingModelTests.cpp @@ -56,9 +56,11 @@ namespace binding ConfiguredBindingModel::~ConfiguredBindingModel() { delete _binding; - delete[] _extFuns; delete[] _boundOffset; + for (cadet::IExternalFunction*& ef : _extFuns) + delete ef; + ::operator delete(_bufferMemory); } @@ -86,8 +88,11 @@ ConfiguredBindingModel ConfiguredBindingModel::create(const char* name, unsigned } // Assign external functions - cadet::IExternalFunction* extFuns = new LinearExternalFunction[50]; - bm->setExternalFunctions(&extFuns, 50); + std::vector extFuns(50, nullptr); + for (int i = 0; i < 50; ++i) + extFuns[i] = new LinearExternalFunction(); + + bm->setExternalFunctions(extFuns.data(), 50); // Allocate memory buffer unsigned int requiredMem = 0; @@ -103,7 +108,7 @@ ConfiguredBindingModel ConfiguredBindingModel::create(const char* name, unsigned std::memset(buffer, 0, requiredMem); } - return ConfiguredBindingModel(bm, nComp, nBound, boundOffset, buffer, bufferEnd, extFuns); + return ConfiguredBindingModel(bm, nComp, nBound, boundOffset, buffer, bufferEnd, std::move(extFuns)); } ConfiguredBindingModel ConfiguredBindingModel::create(const char* name, unsigned int nComp, unsigned int const* nBound, int const* isKinetic, const char* config) @@ -130,8 +135,11 @@ ConfiguredBindingModel ConfiguredBindingModel::create(const char* name, unsigned } // Assign external functions - cadet::IExternalFunction* extFuns = new LinearExternalFunction[50]; - bm->setExternalFunctions(&extFuns, 50); + std::vector extFuns(50, nullptr); + for (int i = 0; i < 50; ++i) + extFuns[i] = new LinearExternalFunction(); + + bm->setExternalFunctions(extFuns.data(), 50); // Allocate memory buffer unsigned int requiredMem = 0; @@ -147,7 +155,7 @@ ConfiguredBindingModel ConfiguredBindingModel::create(const char* name, unsigned std::memset(buffer, 0, requiredMem); } - return ConfiguredBindingModel(bm, nComp, nBound, boundOffset, buffer, bufferEnd, extFuns); + return ConfiguredBindingModel(bm, nComp, nBound, boundOffset, buffer, bufferEnd, std::move(extFuns)); } void ConfiguredBindingModel::increaseBufferSize(int inc) diff --git a/test/BindingModelTests.hpp b/test/BindingModelTests.hpp index 2a49cf014..158e5eff1 100644 --- a/test/BindingModelTests.hpp +++ b/test/BindingModelTests.hpp @@ -61,13 +61,12 @@ namespace binding public: ConfiguredBindingModel(ConfiguredBindingModel&& cpy) CADET_NOEXCEPT - : _binding(cpy._binding), _nComp(cpy._nComp), _nBound(cpy._nBound), _boundOffset(cpy._boundOffset), _buffer(std::move(cpy._buffer)), _bufferMemory(cpy._bufferMemory), _extFuns(cpy._extFuns) + : _binding(cpy._binding), _nComp(cpy._nComp), _nBound(cpy._nBound), _boundOffset(cpy._boundOffset), _buffer(std::move(cpy._buffer)), _bufferMemory(cpy._bufferMemory), _extFuns(std::move(cpy._extFuns)) { cpy._binding = nullptr; cpy._nBound = nullptr; cpy._boundOffset = nullptr; cpy._bufferMemory = nullptr; - cpy._extFuns = nullptr; } ~ConfiguredBindingModel(); @@ -80,13 +79,12 @@ namespace binding _boundOffset = cpy._boundOffset; _buffer = std::move(cpy._buffer); _bufferMemory = cpy._bufferMemory; - _extFuns = cpy._extFuns; + _extFuns = std::move(cpy._extFuns); cpy._binding = nullptr; cpy._nBound = nullptr; cpy._boundOffset = nullptr; cpy._bufferMemory = nullptr; - cpy._extFuns = nullptr; return *this; } @@ -109,8 +107,8 @@ namespace binding private: - ConfiguredBindingModel(cadet::model::IBindingModel* binding, unsigned int nComp, unsigned int const* nBound, unsigned int const* boundOffset, void* bufferStart, void* bufferEnd, cadet::IExternalFunction* extFuns) - : _binding(binding), _nComp(nComp), _nBound(nBound), _boundOffset(boundOffset), _buffer(bufferStart, bufferEnd), _bufferMemory(bufferStart), _extFuns(extFuns) + ConfiguredBindingModel(cadet::model::IBindingModel* binding, unsigned int nComp, unsigned int const* nBound, unsigned int const* boundOffset, void* bufferStart, void* bufferEnd, std::vector&& extFuns) + : _binding(binding), _nComp(nComp), _nBound(nBound), _boundOffset(boundOffset), _buffer(bufferStart, bufferEnd), _bufferMemory(bufferStart), _extFuns(std::move(extFuns)) { } @@ -120,7 +118,7 @@ namespace binding unsigned int const* _boundOffset; cadet::LinearBufferAllocator _buffer; void* _bufferMemory; - cadet::IExternalFunction* _extFuns; + std::vector _extFuns; }; /**