Details
-
Type:
Bug Detection
-
Status: Active
-
Resolution: Unresolved
-
Labels:None
-
Message:Refactor this code to avoid using a const reference to a string.
-
Default Severity:Major
-
Impact:Low
-
Likelihood:High
-
Default Quality Profiles:Sonar way, MISRA C++ 2008 recommended
-
Targeted languages:C++
-
Remediation Function:Constant/Issue
-
Constant Cost:15min
-
Analysis Scope:Main Sources, Test Sources
Description
It is unsafe to have const string reference members as they can be created from a temporary quite easily.
struct Foo { Foo(const string &Str) : Str(Str) {} const string &Str; }; Foo instance("string");
When the constructor is called, a string temporary is created from const char * and destroyed immediately after. This results in a dangling reference.
This rule applies for both std::string and ::string const reference members.
Attachments
Issue Links
- implements
-
CPP-1692 C++ rule: Do not declare const reference string members
-
- Open
-