Skip to content

Commit dc2cbf7

Browse files
committed
Add tests for always-locked fields
1 parent aed5164 commit dc2cbf7

File tree

3 files changed

+56
-0
lines changed

3 files changed

+56
-0
lines changed
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package test.cwe367.semmle.tests;
2+
3+
import java.util.Enumeration;
4+
import java.util.Hashtable;
5+
6+
class FieldAlwaysLocked {
7+
8+
Hashtable field;
9+
10+
public FieldAlwaysLocked() {
11+
field = new Hashtable();
12+
}
13+
14+
protected synchronized void checkOut() {
15+
Object o;
16+
if (field.size() > 0) {
17+
Enumeration e = field.keys(); // $ SPURIOUS: Alert
18+
while (e.hasMoreElements()) {
19+
o = e.nextElement();
20+
field.remove(o); // $ SPURIOUS: Alert
21+
}
22+
}
23+
}
24+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package test.cwe367.semmle.tests;
2+
3+
import java.util.Enumeration;
4+
import java.util.Hashtable;
5+
6+
class FieldNotAlwaysLocked {
7+
8+
Hashtable field;
9+
10+
public FieldNotAlwaysLocked() {
11+
field = new Hashtable();
12+
}
13+
14+
protected synchronized void checkOut() {
15+
Object o;
16+
if (field.size() > 0) {
17+
Enumeration e = field.keys(); // $ Alert
18+
while (e.hasMoreElements()) {
19+
o = e.nextElement();
20+
field.remove(o); // $ Alert
21+
}
22+
}
23+
}
24+
25+
protected void modifyUnlocked() {
26+
field = new Hashtable();
27+
}
28+
}

java/ql/test/query-tests/security/CWE-367/semmle/tests/TOCTOURace.expected

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,7 @@
1+
| FieldAlwaysLocked.java:17:41:17:52 | keys(...) | This uses the state of $@ which $@. But these are not jointly synchronized. | FieldAlwaysLocked.java:8:19:8:23 | field | field | FieldAlwaysLocked.java:16:21:16:32 | size(...) | is checked at a previous call |
2+
| FieldAlwaysLocked.java:20:33:20:47 | remove(...) | This uses the state of $@ which $@. But these are not jointly synchronized. | FieldAlwaysLocked.java:8:19:8:23 | field | field | FieldAlwaysLocked.java:16:21:16:32 | size(...) | is checked at a previous call |
3+
| FieldNotAlwaysLocked.java:17:41:17:52 | keys(...) | This uses the state of $@ which $@. But these are not jointly synchronized. | FieldNotAlwaysLocked.java:8:19:8:23 | field | field | FieldNotAlwaysLocked.java:16:21:16:32 | size(...) | is checked at a previous call |
4+
| FieldNotAlwaysLocked.java:20:33:20:47 | remove(...) | This uses the state of $@ which $@. But these are not jointly synchronized. | FieldNotAlwaysLocked.java:8:19:8:23 | field | field | FieldNotAlwaysLocked.java:16:21:16:32 | size(...) | is checked at a previous call |
15
| Test.java:13:4:13:10 | act(...) | This uses the state of $@ which $@. But these are not jointly synchronized. | Test.java:10:32:10:41 | r | r | Test.java:12:7:12:18 | getState(...) | is checked at a previous call |
26
| Test.java:20:4:20:10 | act(...) | This uses the state of $@ which $@. But these are not jointly synchronized. | Test.java:17:32:17:42 | r | r | Test.java:19:7:19:18 | getState(...) | is checked at a previous call |
37
| Test.java:27:4:27:10 | act(...) | This uses the state of $@ which $@. But these are not jointly synchronized. | Test.java:24:19:24:28 | r | r | Test.java:26:7:26:18 | getState(...) | is checked at a previous call |

0 commit comments

Comments
 (0)