Skip to content

Commit d18bbf6

Browse files
committed
C#: Make query only apply to reftypes, since I believe valuetypes are safe or cannot be fixed trivially using the volatile keyword.
1 parent b473d2f commit d18bbf6

File tree

3 files changed

+7
-9
lines changed

3 files changed

+7
-9
lines changed

csharp/ql/src/Concurrency/UnsafeLazyInitialization.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
* @description A repeated check on a non-volatile field is not thread-safe on some platforms,
44
* and could result in unexpected behavior.
55
* @kind problem
6-
* @problem.severity recommendation
6+
* @problem.severity error
77
* @precision medium
88
* @id cs/unsafe-double-checked-lock
99
* @tags correctness
@@ -40,5 +40,5 @@ where
4040
doubleCheckedLock(field, ifs) and
4141
not field.isVolatile() and
4242
exists(VariableWrite write | write = ifs.getThen().getAChild+() and write.getTarget() = field) and
43-
not field.getType() instanceof Struct
43+
field.getType() instanceof RefType
4444
select ifs, "Field $@ should be 'volatile' for this double-checked lock.", field, field.getName()

csharp/ql/test/query-tests/Concurrency/UnsafeLazyInitialization/UnsafeLazyInitialization.cs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ void Fn()
3131
if (obj1 == null)
3232
obj1 = null;
3333

34-
// BAD
34+
// GOOD: A value-type
3535
if (cond1)
3636
lock (mutex)
3737
if (cond1)
@@ -132,9 +132,9 @@ void Fn()
132132
}
133133

134134
// BAD: Field x should be volatile
135-
if (struct1.x == 2)
135+
if (struct1.x is null)
136136
lock (mutex)
137-
if(struct1.x == 2)
137+
if(struct1.x is null)
138138
struct1.x = 3;
139139

140140
// GOOD: Tuples are structs so cannot be volatile.
@@ -151,7 +151,7 @@ void Fn()
151151

152152
struct Coord
153153
{
154-
public int x, y;
154+
public object x, y;
155155

156156
public static bool operator==(Coord c1, Coord c2) => c1.x==c2.x && c1.y == c2.y;
157157
public static bool operator!=(Coord c1, Coord c2) => !(c1==c2);
Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
| UnsafeLazyInitialization.cs:17:9:26:9 | if (...) ... | Field $@ should be 'volatile' for this double-checked lock. | UnsafeLazyInitialization.cs:6:19:6:22 | obj1 | obj1 |
22
| UnsafeLazyInitialization.cs:29:9:32:32 | if (...) ... | Field $@ should be 'volatile' for this double-checked lock. | UnsafeLazyInitialization.cs:6:19:6:22 | obj1 | obj1 |
3-
| UnsafeLazyInitialization.cs:35:9:38:34 | if (...) ... | Field $@ should be 'volatile' for this double-checked lock. | UnsafeLazyInitialization.cs:9:10:9:14 | cond1 | cond1 |
43
| UnsafeLazyInitialization.cs:80:9:88:9 | if (...) ... | Field $@ should be 'volatile' for this double-checked lock. | UnsafeLazyInitialization.cs:6:19:6:22 | obj1 | obj1 |
54
| UnsafeLazyInitialization.cs:98:9:107:9 | if (...) ... | Field $@ should be 'volatile' for this double-checked lock. | UnsafeLazyInitialization.cs:6:19:6:22 | obj1 | obj1 |
65
| UnsafeLazyInitialization.cs:110:9:120:9 | if (...) ... | Field $@ should be 'volatile' for this double-checked lock. | UnsafeLazyInitialization.cs:6:19:6:22 | obj1 | obj1 |
76
| UnsafeLazyInitialization.cs:110:9:120:9 | if (...) ... | Field $@ should be 'volatile' for this double-checked lock. | UnsafeLazyInitialization.cs:8:12:8:15 | obj3 | obj3 |
8-
| UnsafeLazyInitialization.cs:135:9:138:34 | if (...) ... | Field $@ should be 'volatile' for this double-checked lock. | UnsafeLazyInitialization.cs:154:16:154:16 | x | x |
9-
| UnsafeLazyInitialization.cs:141:9:148:9 | if (...) ... | Field $@ should be 'volatile' for this double-checked lock. | UnsafeLazyInitialization.cs:12:15:12:19 | pair1 | pair1 |
7+
| UnsafeLazyInitialization.cs:135:9:138:34 | if (...) ... | Field $@ should be 'volatile' for this double-checked lock. | UnsafeLazyInitialization.cs:154:19:154:19 | x | x |

0 commit comments

Comments
 (0)