From fafa0aebaf21058fa0142dd91199c9a59a4cf706 Mon Sep 17 00:00:00 2001 From: Jason Marechal Date: Mon, 22 Jul 2024 15:33:53 +0200 Subject: [PATCH] Fix potential access violation --- src/solver/constraints-builder/cbuilder.cpp | 55 ++++++++----------- .../solver/constraints-builder/cbuilder.h | 52 +++++++++--------- src/solver/constraints-builder/load.cpp | 49 ++++++++--------- 3 files changed, 73 insertions(+), 83 deletions(-) diff --git a/src/solver/constraints-builder/cbuilder.cpp b/src/solver/constraints-builder/cbuilder.cpp index 2a1e1b8dce..5c02947a6f 100644 --- a/src/solver/constraints-builder/cbuilder.cpp +++ b/src/solver/constraints-builder/cbuilder.cpp @@ -1,23 +1,23 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator 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 -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator 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 + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #include "antares/solver/constraints-builder/cbuilder.h" @@ -54,15 +54,6 @@ CBuilder::CBuilder(Antares::Data::Study& study): { } -CBuilder::~CBuilder() -{ - // delete all the elements of pLink - for (auto i = pLink.begin(); i != pLink.end(); i++) - { - delete *i; - } -} - bool Antares::CBuilder::isCycleDriver(linkInfo* lnkI) { std::string s1(lnkI->ptr->from->name.to()); @@ -180,7 +171,7 @@ bool CBuilder::updateLinks() } // check validity of loopflow against NTC - if (includeLoopFlow && !checkValidityOfNodalLoopFlow(linkInfo, hour)) + if (includeLoopFlow && !checkValidityOfNodalLoopFlow(linkInfo.get(), hour)) { return false; } @@ -190,8 +181,8 @@ bool CBuilder::updateLinks() continue; } - updateLinkPhaseShift(linkInfo, hour); - if (!checkLinkPhaseShift(linkInfo, hour)) + updateLinkPhaseShift(linkInfo.get(), hour); + if (!checkLinkPhaseShift(linkInfo.get(), hour)) { return false; } @@ -239,7 +230,7 @@ bool CBuilder::update() && ((*linkInfoIt)->type == Antares::Data::atAC /*|| (*linkInfoIt)->type == linkInfo::tyACPST*/)) { - enabledACLines.push_back(*linkInfoIt); + enabledACLines.push_back((*linkInfoIt).get()); } } diff --git a/src/solver/constraints-builder/include/antares/solver/constraints-builder/cbuilder.h b/src/solver/constraints-builder/include/antares/solver/constraints-builder/cbuilder.h index 833f9e82e6..29ddd2b4c5 100644 --- a/src/solver/constraints-builder/include/antares/solver/constraints-builder/cbuilder.h +++ b/src/solver/constraints-builder/include/antares/solver/constraints-builder/cbuilder.h @@ -1,23 +1,23 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator 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 -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator 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 + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #ifndef __ANTARES_CONSTRAINTSBUILDER_BUILDER_CBUILDER_H__ #define __ANTARES_CONSTRAINTSBUILDER_BUILDER_CBUILDER_H__ @@ -231,7 +231,7 @@ class CBuilder final */ CBuilder(Antares::Data::Study&); //! Destructor - ~CBuilder(); + ~CBuilder() = default; //@} /*! @@ -262,7 +262,7 @@ class CBuilder final { auto linkIT = std::find_if(pLink.begin(), pLink.end(), - [&u, &v](const linkInfo* edgeP) -> bool + [&u, &v](std::shared_ptr edgeP) -> bool { if (edgeP->ptr->from->id == u && edgeP->ptr->with->id == v) { @@ -279,7 +279,7 @@ class CBuilder final }); if (linkIT != pLink.end()) { - return *linkIT; + return linkIT->get(); } return nullptr; @@ -294,11 +294,11 @@ class CBuilder final auto a = area.second; std::for_each(pLink.begin(), pLink.end(), - [&a, this](linkInfo* edgeP) + [&a, this](std::shared_ptr edgeP) { if (edgeP->ptr->from == a || edgeP->ptr->with == a) { - this->areaToLinks[a].insert(edgeP); + this->areaToLinks[a].insert(edgeP.get()); } }); } @@ -308,7 +308,7 @@ class CBuilder final { if (i < pLink.size()) { - return pLink[i]; + return pLink[i].get(); } return nullptr; } @@ -418,7 +418,7 @@ class CBuilder final const double& secondMember); public: - Vector pLink; + std::vector> pLink; private: std::string pPrefix; diff --git a/src/solver/constraints-builder/load.cpp b/src/solver/constraints-builder/load.cpp index c0ee4ec3e4..73beec49c1 100644 --- a/src/solver/constraints-builder/load.cpp +++ b/src/solver/constraints-builder/load.cpp @@ -1,23 +1,23 @@ /* -** Copyright 2007-2024, RTE (https://www.rte-france.com) -** See AUTHORS.txt -** SPDX-License-Identifier: MPL-2.0 -** This file is part of Antares-Simulator, -** Adequacy and Performance assessment for interconnected energy networks. -** -** Antares_Simulator is free software: you can redistribute it and/or modify -** it under the terms of the Mozilla Public Licence 2.0 as published by -** the Mozilla Foundation, either version 2 of the License, or -** (at your option) any later version. -** -** Antares_Simulator 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 -** Mozilla Public Licence 2.0 for more details. -** -** You should have received a copy of the Mozilla Public Licence 2.0 -** along with Antares_Simulator. If not, see . -*/ + * Copyright 2007-2024, RTE (https://www.rte-france.com) + * See AUTHORS.txt + * SPDX-License-Identifier: MPL-2.0 + * This file is part of Antares-Simulator, + * Adequacy and Performance assessment for interconnected energy networks. + * + * Antares_Simulator is free software: you can redistribute it and/or modify + * it under the terms of the Mozilla Public Licence 2.0 as published by + * the Mozilla Foundation, either version 2 of the License, or + * (at your option) any later version. + * + * Antares_Simulator 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 + * Mozilla Public Licence 2.0 for more details. + * + * You should have received a copy of the Mozilla Public Licence 2.0 + * along with Antares_Simulator. If not, see . + */ #include #include @@ -43,19 +43,18 @@ bool CBuilder::completeFromStudy() { // for all links of the study // check if it has been enabled in the INI File - std::shared_ptr k( - findLinkInfoFromNodeNames(j->second->from->id, j->second->with->id)); + auto k = findLinkInfoFromNodeNames(j->second->from->id, j->second->with->id); if (!k) { - k = std::make_shared(); + auto infos = std::make_shared(); logs.info() << "Read data (link " << nCount++ << ")"; // if Yes, complete the linkInfo // load the pointer - k->ptr = j->second; - k->type = k->ptr->assetType; + infos->ptr = j->second; + infos->type = infos->ptr->assetType; - pLink.push_back(k.get()); + pLink.push_back(infos); } } }