Skip to content

Commit b473d2f

Browse files
committed
C#: Update change notes. Decrease the priority of this query because the volatile keyword is no longer needed on modern .Net runtimes.
1 parent 7addd41 commit b473d2f

File tree

3 files changed

+30
-21
lines changed

3 files changed

+30
-21
lines changed

change-notes/1.20/analysis-csharp.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
| Use of default ToString() (cs/call-to-object-tostring) | Fewer false positives | Results have been removed for `char` arrays passed to `StringBuilder.Append()`, which were incorrectly marked as using `ToString`. |
1919
| Use of default ToString() (cs/call-to-object-tostring) | Fewer results | Results have been removed when the object is an interface or an abstract class. |
2020
| Unused format argument (cs/format-argument-unused) | Fewer false positives | Results have been removed where the format string is empty. This is often used as a default value and is not an interesting result. |
21-
21+
| Double-checked lock is not thread-safe (cs/unsafe-double-checked-lock) | Fewer false positives, more true positives | Results have been removed where the underlying field was not updated in the `lock` statement, or where the field is a `struct`. Results have been added where there are other statements inside the `lock` statement. |
22+
2223
## Changes to code extraction
2324

2425
* Fix extraction of `for` statements where the condition declares new variables using `is`.

csharp/ql/src/Concurrency/UnsafeLazyInitialization.qhelp

Lines changed: 24 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<!DOCTYPE qhelp SYSTEM "qhelp.dtd">
22
<qhelp>
33
<overview>
4-
<p>Double-checked locking requires that the underlying field is <code>volatile</code>,
5-
otherwise the program can behave incorrectly when running in multiple threads,
4+
<p>Double-checked locking requires that the underlying field is <code>volatile</code>,
5+
otherwise the program can behave incorrectly when running in multiple threads,
66
for example by computing the field twice.
77
</p>
88
</overview>
@@ -16,6 +16,7 @@ for example by computing the field twice.
1616
<li>Make the field volatile using the <code>volatile</code> keyword.</li>
1717
<li>Use the <code>System.Lazy</code> class, which is guaranteed to be thread-safe.
1818
This can often lead to more elegant code.</li>
19+
<li>Use <code>System.Threading.LazyInitializer</code>.
1920
</ol>
2021

2122
</recommendation>
@@ -51,19 +52,26 @@ automatically thread-safe (Recommendation 3):</p>
5152
</example>
5253

5354
<references>
54-
55-
<li>
56-
MSDN: <a href="https://msdn.microsoft.com/en-us/library/ff650316.aspx">Implementing Singleton in C#</a>.
57-
</li>
58-
<li>
59-
MSDN Magazine: <a href="https://msdn.microsoft.com/magazine/jj863136">The C# Memory Model in Theory and Practice</a>.
60-
</li>
61-
<li>
62-
MSDN, C# Reference: <a href="https://msdn.microsoft.com/en-us/library/x13ttww7.aspx">volatile</a>.
63-
</li>
64-
<li>
65-
Wikipedia: <a href="https://en.wikipedia.org/wiki/Double-checked_locking">Double-checked locking</a>.
66-
</li>
67-
55+
<li>
56+
MSDN: <a href=https://docs.microsoft.com/en-us/dotnet/api/system.lazy-1">Lazy<T> Class</T></a>.
57+
</li>
58+
<li>
59+
MSDN: <a href="https://docs.microsoft.com/en-us/dotnet/api/system.threading.lazyinitializer.ensureinitialized">LazyInitializer.EnsureInitialized Method</a>.
60+
</li>
61+
<li>
62+
MSDN: <a href="https://docs.microsoft.com/en-us/dotnet/api/system.threading.lazyinitializer.ensureinitialized">LazyInitializer.EnsureInitialized Method</a>.
63+
</li>
64+
<li>
65+
MSDN: <a href="https://msdn.microsoft.com/en-us/library/ff650316.aspx">Implementing Singleton in C#</a>.
66+
</li>
67+
<li>
68+
MSDN Magazine: <a href="https://msdn.microsoft.com/magazine/jj863136">The C# Memory Model in Theory and Practice</a>.
69+
</li>
70+
<li>
71+
MSDN, C# Reference: <a href="https://msdn.microsoft.com/en-us/library/x13ttww7.aspx">volatile</a>.
72+
</li>
73+
<li>
74+
Wikipedia: <a href="https://en.wikipedia.org/wiki/Double-checked_locking">Double-checked locking</a>.
75+
</li>
6876
</references>
6977
</qhelp>

csharp/ql/src/Concurrency/UnsafeLazyInitialization.ql

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
/**
22
* @name Double-checked lock is not thread-safe
3-
* @description A repeated check on a non-volatile field is not thread-safe, and
4-
* could result in unexpected behavior.
3+
* @description A repeated check on a non-volatile field is not thread-safe on some platforms,
4+
* and could result in unexpected behavior.
55
* @kind problem
6-
* @problem.severity error
7-
* @precision high
6+
* @problem.severity recommendation
7+
* @precision medium
88
* @id cs/unsafe-double-checked-lock
99
* @tags correctness
1010
* concurrency

0 commit comments

Comments
 (0)