Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADIOS2 Access Mode: Fix Dangling Ref #1674

Merged
merged 4 commits into from
Oct 7, 2024

Conversation

ax3l
Copy link
Member

@ax3l ax3l commented Oct 2, 2024

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();
      |                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~

First seen in #1672

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();
      |                                        ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~
```
@@ -1528,7 +1528,7 @@ adios2::Mode ADIOS2IOHandlerImpl::adios2AccessMode(std::string const &fullPath)
if (m_config.json().contains("engine") &&
m_config["engine"].json().contains("access_mode"))
{
auto const &access_mode_json = m_config["engine"]["access_mode"].json();
auto const access_mode_json = m_config["engine"]["access_mode"].json();
Copy link
Member Author

@ax3l ax3l Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe because of the extra [][] that can be dangling? Or is this a false positive in GCC 13.3 -Wdangling-reference?

The warning states:

the temporary was destroyed at the end of the full expression 'openPMD::json::TracingJSON::operator with Key = const char (&)[12].openPMD::json::TracingJSON::json()'

It looks like TracingJSON is indeed returned by value from operator[], so this might be a bug indeed...?

Copy link
Member Author

@ax3l ax3l Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see similar patterns in:

src/IO/HDF5/ParallelHDF5IOHandler.cpp:234:                    auto const &val = vfd_json_config[key].json();
src/IO/HDF5/ParallelHDF5IOHandler.cpp:253:                    auto const &val = vfd_json_config[key].json();
src/IO/ADIOS/ADIOS2IOHandler.cpp:327:    nlohmann::json const &operators = _operators.json();

but no warnings about them. Maybe worth to double check.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a known false positive in gcc13 https://gcc.gnu.org/bugzilla/show_bug.cgi?id=109642
TracingJSON::operator[] creates new handles for the same data, i.e. within the handle, the actual data is behind a pointer, but the handle additionally contains position information on where we are within the JSON dataset.

    class TracingJSON
    {
        // ...
    private:
        /**
         * @brief The JSON object with which this class has been initialized.
         *        Shared pointer shared between all instances returned by
         *        operator[]() in order to avoid use-after-free situations.
         *
         */
        std::shared_ptr<nlohmann::json> m_originalJSON;

        // ...        

        /**
         * @brief The sub-expression within m_originalJSON corresponding with
         *        the current instance.
         *
         */
        nlohmann::json *m_positionInOriginal;
       
        // ...

    };

gcc13 only sees that operator[] returns by value and does not know that the class is reference-like.
We could either solve this by doing a (useless) copy, or by temporarily deactivating the warning (this class is used only internally and not included in public headers).

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Found an easier solution, see last commit

Copy link
Member Author

@ax3l ax3l Oct 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks! Oh, allright!

The original looks a bit better though (and has probably faster compile time) :) Do we want to just keep it as is? :)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would still like to solve this at an API level, so we have an API that gcc actually understands. If we keep it as it is, then we will get bug reports and have to tell people it's a compiler bug. I changed it into a solution now that does not need templates.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good point

@ax3l ax3l merged commit e9d8999 into openPMD:dev Oct 7, 2024
30 of 31 checks passed
@ax3l ax3l deleted the fix-dangling-string-access-mode branch October 7, 2024 23:30
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants