CONST-member-ret (C++ only)
Synopsis
A member function qualified as const returns a pointer member variable.
Enabled by default
Yes
Severity/Certainty
Medium/Medium

Full description
A member function qualified as const returns a pointer member variable. This might violate the semantics of the function's const qualification, as the data at that address might be overwritten, or the memory itself might be freed. This will not be identified by a compiler, because the pointer being returned is a copy even though the memory to which it refers is vulnerable. This check is identical to MISRAC++2008-9-3-1.
Coding standards
- MISRA C++ 2008 9-3-1
(Required) const member functions shall not return non-const pointers or references to class-data.
Code examples
The following code example fails the check and will give a warning:
class C{
int* foo() const {
return p;
}
int* p;
};
The following code example passes the check and will not give a warning about this issue:
class C{
int* foo() {
return p;
}
int* p;
};