error get_error() {
std::array<char, 1024> message {};
auto code = fake_get_error(message); // places error description in `message` and returns error code
return { code, message.data() };
}
given that error::message is a std::string whose constructor makes a copy of a [const] char* argument, the following is a false positive:
returnDanglingLifetime | 562 | error | Returning object that points to local variable 'message' that will be invalid when returning.
I wrote a test to prove that the error struct contains a copy, rather than pointing to the stack-based std::array. The test scopes the error outside of the scope of the class that implements get_error, and the message showing its contents is emitted after the object containing the stack-based array has gone out of scope:
Given the following struct:
and the following function returning said struct:
given that
error::message
is astd::string
whose constructor makes a copy of a[const] char*
argument, the following is a false positive:I wrote a test to prove that the
error
struct contains a copy, rather than pointing to the stack-basedstd::array
. The test scopes theerror
outside of the scope of the class that implementsget_error
, and the message showing its contents is emitted after the object containing the stack-based array has gone out of scope:Here's the output:
I can provide the source that you can use to reproduce this if you need it.