Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify rule S2445: Add C# description and samples #1570

Merged
merged 15 commits into from Mar 1, 2023
Merged
4 changes: 4 additions & 0 deletions rules/S2445/csharp/highlighting.adoc
@@ -0,0 +1,4 @@
=== Highlighting

locked object in `lock (xxx)` statement

10 changes: 10 additions & 0 deletions rules/S2445/csharp/message.adoc
@@ -0,0 +1,10 @@
=== Message

Do not lock on writable field "xxx", use a readonly field instead.

Do not lock on local variable "xxx", use a readonly field instead.

Do not lock on a new instance because is a no-op, use a readonly field instead.

Do not lock on strings as they can be interned, use a readonly field instead.

2 changes: 1 addition & 1 deletion rules/S2445/csharp/metadata.json
@@ -1,3 +1,3 @@
{

}
66 changes: 63 additions & 3 deletions rules/S2445/csharp/rule.adoc
@@ -1,14 +1,74 @@
include::../rule.adoc[]
Locking on a class field synchronizes not on the field itself, but on the object assigned to it. Thus, there are some good practices to follow to avoid bugs related to thread synchronization.

1. Locking on a non-`readonly` field makes it possible for the field's value to change while a thread is in the code block locked on the old value. This allows another thread to lock on the new value and access the same block concurrently.

2. Locking on a local variable or a new instance of an object can undermine synchronization because two different threads running the same method in parallel will potentially lock on different instances of the same object, allowing them to access the synchronized block at the same time.

3. Locking on a string literal is also dangerous since, depending on whether the string is interned or not, different threads may or may not synchronize on the same object instance.


== Noncompliant Code Example

[source,csharp]
----
private Color color = new Color("red");
private readonly string colorString = "red";

private void DoSomething()
{
// Synchronizing access via "color"
lock (color) // Noncompliant; lock is actually on object instance "red" referred to by the "color" field
{
//...
color = new Color("green"); // other threads now allowed into this block
// ...
}
lock (new object()) // Noncompliant; this is a no-op
{
// ...
}
lock (colorString) // Noncompliant; strings can be interned
{
// ...
}
}
----


== Compliant Solution

[source,csharp]
----
private Color color = new Color("red");
private readonly object lockObj = new object();

private void DoSomething()
{
lock (lockObj)
{
//...
color = new Color("green");
// ...
}
}
----

mary-georgiou-sonarsource marked this conversation as resolved.
Show resolved Hide resolved
== See
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved

* https://learn.microsoft.com/en-us/dotnet/csharp/language-reference/statements/lock[Lock Statement] - lock statement - ensure exclusive access to a shared resource
* https://learn.microsoft.com/en-us/dotnet/api/system.string.intern[String.Intern] - `String.Intern(String)` Method
* https://cwe.mitre.org/data/definitions/412[MITRE, CWE-412] - Unrestricted Externally Accessible Lock
* https://cwe.mitre.org/data/definitions/413[MITRE, CWE-413] - Improper Resource Locking

ifdef::env-github,rspecator-view[]

'''
== Implementation Specification
(visible only on this page)

include::../message.adoc[]
include::message.adoc[]
pavel-mikula-sonarsource marked this conversation as resolved.
Show resolved Hide resolved

include::../highlighting.adoc[]
include::highlighting.adoc[]

'''
== Comments And Links
Expand Down
File renamed without changes.
File renamed without changes.
1 change: 1 addition & 0 deletions rules/S2445/java/metadata.json
@@ -1,4 +1,5 @@
{
"title": "Blocks should be synchronized on \"private final\" fields",
"tags": [
"cwe",
"multi-threading",
Expand Down
45 changes: 42 additions & 3 deletions rules/S2445/java/rule.adoc
@@ -1,4 +1,43 @@
include::../rule-except-see.adoc[]
Synchronizing on a class field synchronizes not on the field itself, but on the object assigned to it. So synchronizing on a non-``++final++`` field makes it possible for the field's value to change while a thread is in a block synchronized on the old value. That would allow a second thread, synchronized on the new value, to enter the block at the same time.
mary-georgiou-sonarsource marked this conversation as resolved.
Show resolved Hide resolved


The story is very similar for synchronizing on parameters; two different threads running the method in parallel could pass two different object instances in to the method as parameters, completely undermining the synchronization.


== Noncompliant Code Example

[source,text]
----
private String color = "red";

private void doSomething(){
synchronized(color) { // Noncompliant; lock is actually on object instance "red" referred to by the color variable
//...
color = "green"; // other threads now allowed into this block
// ...
}
synchronized(new Object()) { // Noncompliant this is a no-op.
// ...
}
}
----


== Compliant Solution

[source,text]
----
private String color = "red";
private final Object lockObj = new Object();

private void doSomething(){
synchronized(lockObj) {
//...
color = "green";
// ...
}
}
----

include::see.adoc[]

Expand All @@ -8,9 +47,9 @@ ifdef::env-github,rspecator-view[]
== Implementation Specification
(visible only on this page)

include::../message.adoc[]
include::message.adoc[]

include::../highlighting.adoc[]
include::highlighting.adoc[]

'''
== Comments And Links
Expand Down
2 changes: 1 addition & 1 deletion rules/S2445/metadata.json
@@ -1,5 +1,5 @@
{
"title": "Blocks should be synchronized on \"private final\" fields",
"title": "Blocks should be synchronized on read-only fields",
"type": "BUG",
"status": "ready",
"remediation": {
Expand Down
42 changes: 0 additions & 42 deletions rules/S2445/rule-except-see.adoc

This file was deleted.

3 changes: 0 additions & 3 deletions rules/S2445/rule.adoc

This file was deleted.

5 changes: 0 additions & 5 deletions rules/S2445/see.adoc

This file was deleted.