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
inputStringRefto the private referencemyStringRef.Assigns the pointer parameter
inputStringPointerto 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 inputinputStringRefby 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.
Seleccione un país/idioma
Seleccione un país/idioma para obtener contenido traducido, si está disponible, y ver eventos y ofertas de productos y servicios locales. Según su ubicación geográfica, recomendamos que seleccione: .
También puede seleccionar uno de estos países/idiomas:
Cómo obtener el mejor rendimiento
Seleccione China (en idioma chino o inglés) para obtener el mejor rendimiento. Los sitios web de otros países no están optimizados para ser accedidos desde su ubicación geográfica.
América
- América Latina (Español)
- Canada (English)
- United States (English)
Europa
- 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)