Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Modify rule S2445: Add C# specifics, Move Java specifics
- Loading branch information
1 parent
8a5ec88
commit 685319b
Showing
7 changed files
with
63 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
=== Message | ||
|
||
"xxx" is not "private readonly", and should not be used for locking. | ||
|
||
"xxx" is a local variable, and should not be used for locking. | ||
|
||
Locking on a new instance is a no-op. | ||
|
||
Strings can be interned, and should not be used for locking. | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
{ | ||
|
||
"title": "Blocks should be synchronized on \"private readonly\" fields" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
Locking on a class field synchronizes not on the field itself, but on the object assigned to it. So locking on a non-``++readonly++`` field makes it possible for the field's value to change while a thread is in a block locked on the old value. That would allow a second thread, locked on the new value, to enter the block at the same time. | ||
|
||
|
||
The story is very similar for locking on a local variable or on a new instance; two different threads running the method in parallel would lock on two different object instances, completely undermining the synchronization. | ||
|
||
|
||
== Noncompliant Code Example | ||
|
||
[source,csharp] | ||
---- | ||
private string color = "red"; | ||
private void DoSomething() | ||
{ | ||
lock (color) // Noncompliant; lock is actually on object instance "red" referred to by the color variable | ||
{ | ||
//... | ||
color = "green"; // other threads now allowed into this block | ||
// ... | ||
} | ||
lock (new object()) // Noncompliant this is a no-op. | ||
{ | ||
// ... | ||
} | ||
} | ||
---- | ||
|
||
|
||
== Compliant Solution | ||
|
||
[source,csharp] | ||
---- | ||
private string color = "red"; | ||
private readonly object lockObj = new object(); | ||
private void DoSomething() | ||
{ | ||
lock (lockObj) | ||
{ | ||
//... | ||
color = "green"; | ||
// ... | ||
} | ||
} | ||
---- | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
include::../rule-except-see.adoc[] | ||
include::rule-except-see.adoc[] | ||
|
||
include::see.adoc[] | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1 @@ | ||
include::rule-except-see.adoc[] | ||
|
||
include::see.adoc[] |