open-simulation-platform / libcosim

OSP C++ co-simulation library
https://open-simulation-platform.github.io/libcosim
Mozilla Public License 2.0
61 stars 10 forks source link

Dangling reference in `parse_scenario()` #760

Closed kyllingstad closed 5 months ago

kyllingstad commented 5 months ago

Trying to compile libcosim with GCC 14, I get the following error message:

[...]/src/cosim/scenario_parser.cpp: In function ‘cosim::scenario::scenario cosim::parse_scenario(const std::filesystem::__cxx11::path&, const std::unordered_map<int, manipulable*>&)’:
[...]/src/cosim/scenario_parser.cpp:214:21: error: possibly dangling reference to a temporary [-Werror=dangling-reference]
  214 |         const auto& [index, simulator] =
      |                     ^~~~~~~~~~~~~~~~~~
[...]/src/cosim/scenario_parser.cpp:215:27: note: the temporary was destroyed at the end of the full expression ‘cosim::{anonymous}::find_simulator((* & simulators), cosim::{anonymous}::specified_or_default(const YAML::Node&, const std::string&, std::optional<std::__cxx11::basic_string<char> >)(std::__cxx11::basic_string<char>(((const char*)"model"), std::allocator<char>()), std::optional<std::__cxx11::basic_string<char> >(defaultOpts.cosim::{anonymous}::defaults::model)))’
  215 |             find_simulator(simulators, specified_or_default(event, "model", defaultOpts.model));
      |             ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

In other words, the problem is that find_simulator() returns by value, not by reference, but we still try to capture the result by reference.

The fix is simple: Just drop the & in line 214.