CWE Rule 496
Description
Rule Description
Assigning public data to a private array is equivalent to giving public access to the array.
Polyspace Implementation
The rule checker checks for the issue Public data assigned to private pointer.
Examples
The issue Public data assigned to private pointer occurs when a public member function assigns a public pointer or reference parameter to a private pointer or reference data member. For instance:
class MyClass {
private:
std::string& myStringRef;
std::string* myStringPointer;
public:
MyClass(std::string& inputStringRef, std::string* inputStringPointer)
: myStringRef(inputStringRef), myStringPointer(inputStringPointer) //Noncompliant
{}
};
void foo(){
std::string str1{"string ref"};
std::string str2{"string pointer"};
MyClass a{str1, &str2};
str1 = "Changed string";
}
MyClass:;MyClass()
:
Assigns the reference parameter
inputStringRef
to the private referencemyStringRef
.Assigns the pointer parameter
inputStringPointer
to the private pointermyStringPointer
.
These assignments violate this rule and Polyspace® reports violations.
Polyspace does not report a violation of this rule for function pointers.
If you initialize a private pointer or reference data member by using a public
parameter, then any function can modify the private data member. In the preceding code,
the objects MyClass::myStringRef
and
MyClass::myStringPointer
are private data members. Because
MyClass::MyClass()
assigns public
parameters to
private pointer or reference data members, the function foo()
can
modify these private data members. For instance, str1 = "Changed
string"
sets the value of a.myStringRef
to
"Changed string"
, even though MyClass::myStringRef
is a private field.
Avoid assigning pointer of reference parameters to private pointer or reference data
members. For public functions that set private pointers and reference data members, accept
parameters by value. If possible, set private pointer and reference data members by using
private
setter, which can in turn be invoked from methods from a
friend
method.
In this example, the class MyClass
manages the private reference
data member myStringRef
and the private pointer
myStringPointer
. In the constructor of MyClass
,
public parameters are assigned tomyStringRef
and
myStringPointer
. As a result, the assignment operations in
foo()
changes the value of a.myStringRef
and
a.myStringPointer
. This behavior is unexpected. Polyspace reports a violation of this rule on the declaration of the class
constructor.
#include <string>
class MyClass
{
private:
std::string& myStringRef;
std::string* myStringPointer;
public:
MyClass(std::string& inputStringRef, std::string* inSP)
: myStringRef(inputStringRef), myStringPointer(inSP) //Noncompliant
{}
};
void foo()
{
std::string str1{"string ref"};
std::string str2{"string pointer"};
MyClass a{str1, &str2};
str1 = "Changed string";
str2 = "Another changed string";
}
To fix the violation:
In
MyClass::MyClass()
, take the inputinputStringRef
by value instead of by reference.Use the private setter function
setmyStringPointer()
to set the private pointermyStringPointer
.
#include <string>
class MyClass {
friend void foo();
private:
std::string& myStringRef;
std::string* myStringPointer;
void setmyStringPointer(std::string* inSP){
myStringPointer=inSP; //Compliant
}
public:
MyClass(std::string inputStringRef)
: myStringRef(inputStringRef) //Compliant
{}
};
void foo(){
std::string str1{"string ref"};
std::string str2{"string pointer"};
MyClass a{str1};
a.setmyStringPointer(&str2);
str1 = "Changed string";
str2 = "Another changed string";
}
Check Information
Category: Others |
Version History
Introduced in R2023b
See Also
External Websites
MATLAB Command
You clicked a link that corresponds to this MATLAB command:
Run the command by entering it in the MATLAB Command Window. Web browsers do not support MATLAB commands.
웹사이트 선택
번역된 콘텐츠를 보고 지역별 이벤트와 혜택을 살펴보려면 웹사이트를 선택하십시오. 현재 계신 지역에 따라 다음 웹사이트를 권장합니다:
또한 다음 목록에서 웹사이트를 선택하실 수도 있습니다.
사이트 성능 최적화 방법
최고의 사이트 성능을 위해 중국 사이트(중국어 또는 영어)를 선택하십시오. 현재 계신 지역에서는 다른 국가의 MathWorks 사이트 방문이 최적화되지 않았습니다.
미주
- América Latina (Español)
- Canada (English)
- United States (English)
유럽
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom (English)