Debian Codegraph @

clang-tools-extra/docs/clang-tidy/checks/bugprone/return-const-ref-from-parameter.rst

raw
1 .. title:: clang-tidy - bugprone-return-const-ref-from-parameter
2
3 bugprone-return-const-ref-from-parameter
4 ========================================
5
6 Detects return statements that return a constant reference parameter as constant
7 reference. This may cause use-after-free errors if the caller uses xvalues as
8 arguments.
9
10 In C++, constant reference parameters can accept xvalues which will be destructed
11 after the call. When the function returns such a parameter also as constant reference,
12 then the returned reference can be used after the object it refers to has been
13 destroyed.
14
15 Example
16 -------
17
18 .. code-block:: c++
19
20 struct S {
21 int v;
22 S(int);
23 ~S();
24 };
25
26 const S &fn(const S &a) {
27 return a;
28 }
29
30 const S& s = fn(S{1});
31 s.v; // use after free