Skip to content

Commit da04673

Browse files
committed
Fix query java/internal-representation-exposure regarding generic callees, and add a test
1 parent c149754 commit da04673

File tree

5 files changed

+85
-2
lines changed

5 files changed

+85
-2
lines changed

java/ql/src/Violations of Best Practice/Implementation Hiding/ExposeRepresentation.ql

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ predicate mayWriteToArray(Expr modified) {
7272
// return __array__; ... method()[1] = 0
7373
exists(ReturnStmt rs | modified = rs.getResult() and relevantType(modified.getType()) |
7474
exists(Callable enclosing, MethodAccess ma |
75-
enclosing = rs.getEnclosingCallable() and ma.getMethod() = enclosing
75+
enclosing = rs.getEnclosingCallable() and ma.getMethod().getSourceDeclaration() = enclosing
7676
|
7777
mayWriteToArray(ma)
7878
)
@@ -100,7 +100,7 @@ VarAccess varPassedInto(Callable c, int i) {
100100
predicate exposesByReturn(Callable c, Field f, Expr why, string whyText) {
101101
returnsArray(c, f) and
102102
exists(MethodAccess ma |
103-
ma.getMethod() = c and ma.getCompilationUnit() != c.getCompilationUnit()
103+
ma.getMethod().getSourceDeclaration() = c and ma.getCompilationUnit() != c.getCompilationUnit()
104104
|
105105
mayWriteToArray(ma) and
106106
why = ma and
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
| ExposesRep.java:11:19:11:28 | getStrings | getStrings exposes the internal representation stored in field strings. The value may be modified $@. | User.java:5:5:5:19 | User.java:5:5:5:19 | after this call to getStrings |
2+
| ExposesRep.java:11:19:11:28 | getStrings | getStrings exposes the internal representation stored in field strings. The value may be modified $@. | User.java:13:12:13:26 | User.java:13:12:13:26 | after this call to getStrings |
3+
| ExposesRep.java:11:19:11:28 | getStrings | getStrings exposes the internal representation stored in field strings. The value may be modified $@. | User.java:38:12:38:26 | User.java:38:12:38:26 | after this call to getStrings |
4+
| ExposesRep.java:13:30:13:41 | getStringMap | getStringMap exposes the internal representation stored in field stringMap. The value may be modified $@. | User.java:9:5:9:21 | User.java:9:5:9:21 | after this call to getStringMap |
5+
| ExposesRep.java:17:15:17:24 | setStrings | setStrings exposes the internal representation stored in field strings. The value may be modified $@. | User.java:22:5:22:6 | User.java:22:5:22:6 | through the variable ss |
6+
| ExposesRep.java:21:15:21:26 | setStringMap | setStringMap exposes the internal representation stored in field stringMap. The value may be modified $@. | User.java:27:5:27:5 | User.java:27:5:27:5 | through the variable m |
7+
| ExposesRep.java:29:14:29:21 | getArray | getArray exposes the internal representation stored in field array. The value may be modified $@. | User.java:31:5:31:18 | User.java:31:5:31:18 | after this call to getArray |
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Violations of Best Practice/Implementation Hiding/ExposeRepresentation.ql
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
import java.util.Map;
2+
3+
public class ExposesRep {
4+
private String[] strings;
5+
private Map<String, String> stringMap;
6+
7+
public ExposesRep() {
8+
strings = new String[1];
9+
}
10+
11+
public String[] getStrings() { return strings; }
12+
13+
public Map<String, String> getStringMap() {
14+
return stringMap;
15+
}
16+
17+
public void setStrings(String[] ss) {
18+
this.strings = ss;
19+
}
20+
21+
public void setStringMap(Map<String, String> m) {
22+
this.stringMap = m;
23+
}
24+
}
25+
26+
class GenericExposesRep<T> {
27+
private T[] array;
28+
29+
public T[] getArray() { return array; }
30+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
import java.util.Map;
2+
3+
public class User {
4+
public static void test1(ExposesRep er) {
5+
er.getStrings()[0] = "Hello world";
6+
}
7+
8+
public static void test2(ExposesRep er) {
9+
er.getStringMap().put("Hello", "world");
10+
}
11+
12+
public String[] indirectGetStrings(ExposesRep er) {
13+
return er.getStrings();
14+
}
15+
16+
public void test3(ExposesRep er) {
17+
indirectGetStrings(er)[0] = "Hello world";
18+
}
19+
20+
public static void test4(ExposesRep er, String[] ss) {
21+
er.setStrings(ss);
22+
ss[0] = "Hello world";
23+
}
24+
25+
public static void test5(ExposesRep er, Map<String, String> m) {
26+
er.setStringMap(m);
27+
m.put("Hello", "world");
28+
}
29+
30+
public static void test6(GenericExposesRep<String> ger) {
31+
ger.getArray()[0] = "Hello world";
32+
}
33+
}
34+
35+
class GenericUser<T> {
36+
37+
public String[] indirectGetStrings(ExposesRep er) {
38+
return er.getStrings();
39+
}
40+
41+
public static void test1(ExposesRep er, GenericUser<String> gu) {
42+
gu.indirectGetStrings(er)[0] = "Hello world";
43+
}
44+
45+
}

0 commit comments

Comments
 (0)