Skip to content

Commit e9d8999

Browse files
ax3lfranzpoeschel
andauthored
ADIOS2 Access Mode: Fix Dangling Ref (#1674)
* ADIOS2 Access Mode: Fix Dangling Ref Fix GCC 13.3 warning: ``` /home/runner/work/openPMD-api/openPMD-api/src/IO/ADIOS/ADIOS2IOHandler.cpp: In member function 'adios2::Mode openPMD::ADIOS2IOHandlerImpl::adios2AccessMode(const std::string&)': /home/runner/work/openPMD-api/openPMD-api/src/IO/ADIOS/ADIOS2IOHandler.cpp:1531:21: warning: possibly dangling reference to a temporary [-Wdangling-reference] 1531 | auto const &access_mode_json = m_config["engine"]["access_mode"].json(); | ^~~~~~~~~~~~~~~~ /home/runner/work/openPMD-api/openPMD-api/src/IO/ADIOS/ADIOS2IOHandler.cpp:1531:78: note: the temporary was destroyed at the end of the full expression 'openPMD::json::TracingJSON::operator[](Key&&) [with Key = const char (&)[12]]("access_mode").openPMD::json::TracingJSON::json()' 1531 | auto const &access_mode_json = m_config["engine"]["access_mode"].json(); | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~ ``` * Solve gcc13 false positive at an API level Introduce an explicit API call for the pattern that gcc13 dislikes. * Non-template alternative to the previous solution * Simplify Doxygen --------- Co-authored-by: Franz Pöschel <franz.poeschel@gmail.com>
1 parent 1257ac2 commit e9d8999

File tree

4 files changed

+38
-7
lines changed

4 files changed

+38
-7
lines changed

include/openPMD/auxiliary/JSON_internal.hpp

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,18 @@ namespace json
7676
*
7777
* @return nlohmann::json&
7878
*/
79-
inline nlohmann::json &json()
80-
{
81-
return *m_positionInOriginal;
82-
}
79+
nlohmann::json &json();
80+
81+
/**
82+
* @brief Access the underlying JSON value
83+
*
84+
* See args, first arg, used to distinguish this overload.
85+
*
86+
* @param path Index to some sub-expression. Shortcut for
87+
* `tracingJSON[arg1][arg2][arg3].json()`.
88+
* @return nlohmann::json&
89+
*/
90+
nlohmann::json &json(std::vector<std::string> path);
8391

8492
template <typename Key>
8593
TracingJSON operator[](Key &&key);

src/IO/ADIOS/ADIOS2IOHandler.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1528,7 +1528,7 @@ adios2::Mode ADIOS2IOHandlerImpl::adios2AccessMode(std::string const &fullPath)
15281528
if (m_config.json().contains("engine") &&
15291529
m_config["engine"].json().contains("access_mode"))
15301530
{
1531-
auto const &access_mode_json = m_config["engine"]["access_mode"].json();
1531+
auto const &access_mode_json = m_config.json({"engine", "access_mode"});
15321532
auto maybe_access_mode_string =
15331533
json::asLowerCaseStringDynamic(access_mode_json);
15341534
if (!maybe_access_mode_string.has_value())

src/IO/HDF5/ParallelHDF5IOHandler.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -231,7 +231,7 @@ ParallelHDF5IOHandlerImpl::ParallelHDF5IOHandlerImpl(
231231
{
232232
return std::nullopt;
233233
}
234-
auto const &val = vfd_json_config[key].json();
234+
auto const &val = vfd_json_config.json({key});
235235
if (val.is_number_integer())
236236
{
237237
return val.get<long long>();
@@ -250,7 +250,7 @@ ParallelHDF5IOHandlerImpl::ParallelHDF5IOHandlerImpl(
250250
{
251251
return std::nullopt;
252252
}
253-
auto const &val = vfd_json_config[key].json();
253+
auto const &val = vfd_json_config.json({key});
254254
if (auto str_val = json::asLowerCaseStringDynamic(val);
255255
str_val.has_value())
256256
{

src/auxiliary/JSON.cpp

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -60,6 +60,29 @@ TracingJSON::TracingJSON(ParsedConfig parsedConfig)
6060
std::move(parsedConfig.config), parsedConfig.originallySpecifiedAs}
6161
{}
6262

63+
nlohmann::json &TracingJSON::json()
64+
{
65+
return *m_positionInOriginal;
66+
}
67+
68+
nlohmann::json &TracingJSON::json(std::vector<std::string> paths)
69+
{
70+
if (paths.empty())
71+
{
72+
return json();
73+
}
74+
auto it = paths.begin();
75+
auto end = paths.end();
76+
nlohmann::json *res = &m_positionInOriginal->operator[](*it);
77+
auto subhandle = this->operator[](*it);
78+
for (++it; it != end; ++it)
79+
{
80+
subhandle = subhandle[*it];
81+
res = &(*res)[*it];
82+
}
83+
return *res;
84+
}
85+
6386
nlohmann::json const &TracingJSON::getShadow() const
6487
{
6588
return *m_positionInShadow;

0 commit comments

Comments
 (0)