Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 54950c2

Browse files
committed
Add MethodAccessSystemGetProperty predicate
1 parent 2bb9636 commit 54950c2

5 files changed

Lines changed: 51 additions & 1 deletion

File tree

java/ql/src/semmle/code/java/JDK.qll

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -211,6 +211,21 @@ class MethodSystemGetProperty extends Method {
211211
}
212212
}
213213

214+
/**
215+
* Any method access to a method named `getProperty` on class `java.lang.System`.
216+
*/
217+
class MethodAccessSystemGetProperty extends MethodAccess {
218+
MethodAccessSystemGetProperty() { getMethod() instanceof MethodSystemGetProperty }
219+
220+
/**
221+
* Holds true if this is a compile-time constant call for the specified `propertyName`.
222+
* Eg. `System.getProperty("user.dir")`.
223+
*/
224+
predicate hasCompileTimeConstantGetPropertyName(string propertyName) {
225+
this.getArgument(0).(CompileTimeConstantExpr).getStringValue() = propertyName
226+
}
227+
}
228+
214229
/**
215230
* Any method named `exit` on class `java.lang.Runtime` or `java.lang.System`.
216231
*/

java/ql/src/semmle/code/java/PrintAst.ql

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,5 +16,6 @@ class PrintAstConfigurationOverride extends PrintAstConfiguration {
1616
/**
1717
* TWEAK THIS PREDICATE AS NEEDED.
1818
*/
19-
override predicate shouldPrint(Element e, Location l) { super.shouldPrint(e, l) }
19+
override predicate shouldPrint(Element e, Location l) { super.shouldPrint(e, l) and
20+
not l.getFile().getBaseName().matches("SystemGetPropertyCall.java") }
2021
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
| jdk/SystemGetPropertyCall.java:7:9:7:38 | getProperty(...) |
2+
| jdk/SystemGetPropertyCall.java:11:9:11:46 | getProperty(...) |
3+
| jdk/SystemGetPropertyCall.java:15:9:15:45 | getProperty(...) |
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
/**
2+
* @name SystemCall
3+
* @description Test the definition of System Get Property
4+
*/
5+
6+
import default
7+
8+
from MethodAccessSystemGetProperty ma
9+
where ma.hasCompileTimeConstantGetPropertyName("user.dir")
10+
select ma
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package jdk;
2+
3+
public class SystemGetPropertyCall {
4+
private static final String USER_DIR_PROPERTY = "user.dir";
5+
6+
void a() {
7+
System.getProperty("user.dir");
8+
}
9+
10+
void b() {
11+
System.getProperty("user.dir", "HOME");
12+
}
13+
14+
void c() {
15+
System.getProperty(USER_DIR_PROPERTY);
16+
}
17+
18+
void d() {
19+
System.getProperty("random.property");
20+
}
21+
}

0 commit comments

Comments
 (0)