================ @@ -38,3 +29,21 @@ Example const S& s = fn(S{1}); s.v; // use after free + + +This issue can be resolved by declaring an overload of the problematic function +where the ``const &`` parameter is instead declared as ``&&``. The developer has +to ensure that the implementation of that function does not produce a +use-after-free, the exact error that this check is warning against. +Marking such an ``&&`` overload as ``deleted``, will silence the warning as +well. In the case of different ``const &`` parameters being returned depending +on the control flow of the function, an overload where all problematic +``const &`` parameters have been declared as ``&&`` will resolve the issue. + +This issue can also be resolved by adding ``[[clang::lifetimebound]]`` and +enabling ``-Wdangling`` in clang. See `lifetimebound attribute<https://clang.llvm.org/docs/AttributeReference.html#id11>`_ +for details. + +.. code-block:: c++ + const int &f(const int &a [[clang::lifetimebound]]) { return a; } + const int &v = f(1); // warning: temporary bound to local reference 'v' will be destroyed at the end of the full-expression [-Wdangling] ---------------- 5chmidti wrote:
It would be good to add a comment behind the `f` function, stating that there is no warning by the check in that case. https://github.com/llvm/llvm-project/pull/118315 _______________________________________________ cfe-commits mailing list cfe-commits@lists.llvm.org https://lists.llvm.org/cgi-bin/mailman/listinfo/cfe-commits