CWE Rule 500
Description
Rule Description
An object contains a public static field that is not marked final, which might allow it to be modified in unexpected ways.
Polyspace Implementation
The rule checker checks for Public static field not const.
Examples
Public static field not const
This issue occurs when all of these conditions are true:
A class contains a data member that is both
static
andpublic
The
static
andpublic
data member is notconst
The public
static
data members of a class can be modified by any of class in the
program. Unless public
static
data members are specified as const
, they might
be modified in unexpected ways and result in bugs that are difficult to diagnose.
To resolve this defect, specify the public static
data members as
const
or constexpr
.
#include<string> class SomeAppClass { private: static std::string appPropertiesConfigFilep2; protected: static std::string appPropertiesConfigFilepr2; public: static std::string appPropertiesConfigFile2; //Noncompliant static void foo() {} int bar() { static int v = 3; return v; } };
In this example, the data member appPropertiesConfigFile2
is
static
and public
, but not const
.
Polyspace® flags the member. The static
data members that are
private
and protected
are not flagged. This rule
does not apply to static
objects that are in the scope of
public
member functions of a class.
public static
Data Members as
const
To resolve this defect, declare the public static
data members of a class as const
.
#include<string> class SomeAppClass { private: static std::string appPropertiesConfigFilep2; protected: static std::string appPropertiesConfigFilepr2; public: static const std::string appPropertiesConfigFile2; //No Defect static void foo() {} int bar() { static int v = 3; return v; } };
Check Information
Category: Others |
Version History
Introduced in R2023a
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.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- 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)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)