org.sonar.plugins.csharp.S2696.html Maven / Gradle / Ivy
This rule raises an issue each time a static
field is updated from a non-static method or property.
Why is this an issue?
Updating a static
field from a non-static
method introduces significant challenges and potential bugs. Multiple class
instances and threads can access and modify the static
field concurrently, leading to unintended consequences for other instances or
threads (unexpected behavior, race conditions and
synchronization problems).
class MyClass
{
private static int count = 0;
public void DoSomething()
{
//...
count++; // Noncompliant: make the enclosing instance property 'static' or remove this set on the 'static' field.
}
}
interface MyInterface
{
private static int count = 0;
public void DoSomething()
{
//...
count++; // Noncompliant: remove this set, which updates a 'static' field from an instance method.
}
}
Resources
Documentation
Articles & blog posts
Standards
- STIG Viewer - Application Security and
Development: V-222567 - The application must not be vulnerable to race conditions.
© 2015 - 2024 Weber Informatics LLC | Privacy Policy