Skip to content

Commit a21992a

Browse files
committed
Minor refactoring to improve tests and documentation
1 parent 5b651f2 commit a21992a

File tree

8 files changed

+37
-31
lines changed

8 files changed

+37
-31
lines changed

java/ql/lib/semmle/code/java/environment/SystemProperty.qll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ private class FieldFilePathSeparator extends Field {
7070
* See: https://commons.apache.org/proper/commons-lang/apidocs/org/apache/commons/lang3/SystemUtils.html
7171
*/
7272
private FieldAccess getSystemPropertyFromApacheSystemUtils(string propertyName) {
73-
exists(Field f | f = result.getField() and f.getDeclaringType() instanceof ApacheSystemUtils |
73+
exists(Field f | f = result.getField() and f.getDeclaringType() instanceof TypeApacheSystemUtils |
7474
f.hasName("AWT_TOOLKIT") and propertyName = "awt.toolkit"
7575
or
7676
f.hasName("FILE_ENCODING") and propertyName = "file.encoding"

java/ql/lib/semmle/code/java/frameworks/apache/Lang.qll

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ private class ApacheStrBuilderFluentMethod extends FluentMethod {
4141
/**
4242
* The class `org.apache.commons.lang.SystemUtils` or `org.apache.commons.lang3.SystemUtils`.
4343
*/
44-
class ApacheSystemUtils extends Class {
45-
ApacheSystemUtils() {
44+
class TypeApacheSystemUtils extends Class {
45+
TypeApacheSystemUtils() {
4646
this.hasQualifiedName(["org.apache.commons.lang", "org.apache.commons.lang3"], "SystemUtils")
4747
}
4848
}

java/ql/lib/semmle/code/java/os/OSCheck.qll

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -40,19 +40,18 @@ abstract class IsSpecificUnixVariant extends Guard { }
4040
/**
4141
* Holds when `ma` compares the current OS against the string constant `osString`.
4242
*/
43-
bindingset[osString]
4443
private predicate isOsFromSystemProp(MethodAccess ma, string osString) {
4544
TaintTracking::localExprTaint(getSystemProperty("os.name"), ma.getQualifier()) and // Call from System.getProperty (or equivalent) to some partial match method
4645
exists(StringPartialMatchMethod m, CompileTimeConstantExpr matchedStringConstant |
4746
m = ma.getMethod() and
48-
matchedStringConstant.getStringValue().toLowerCase().matches(osString)
47+
matchedStringConstant.getStringValue().toLowerCase() = osString
4948
|
5049
DataFlow::localExprFlow(matchedStringConstant, ma.getArgument(m.getMatchParameterIndex()))
5150
)
5251
}
5352

5453
private class IsWindowsFromSystemProp extends IsWindowsGuard instanceof MethodAccess {
55-
IsWindowsFromSystemProp() { isOsFromSystemProp(this, "window%") }
54+
IsWindowsFromSystemProp() { isOsFromSystemProp(this, any(string s | s.regexpMatch("windows?"))) }
5655
}
5756

5857
/**
@@ -99,13 +98,15 @@ private class IsUnixFromCharSeparator extends IsUnixGuard {
9998
}
10099

101100
private class IsUnixFromSystemProp extends IsSpecificUnixVariant instanceof MethodAccess {
102-
IsUnixFromSystemProp() { isOsFromSystemProp(this, ["mac%", "linux%"]) }
101+
IsUnixFromSystemProp() {
102+
isOsFromSystemProp(this, any(string s | s.regexpMatch(["mac.*", "linux.*"])))
103+
}
103104
}
104105

105106
bindingset[fieldNamePattern]
106107
private predicate isOsFromApacheCommons(FieldAccess fa, string fieldNamePattern) {
107108
exists(Field f | f = fa.getField() |
108-
f.getDeclaringType() instanceof ApacheSystemUtils and
109+
f.getDeclaringType() instanceof TypeApacheSystemUtils and
109110
f.getName().matches(fieldNamePattern)
110111
)
111112
}

java/ql/test/library-tests/os/Test.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ void testWindows() {
2525
onlyOnWindows();
2626
}
2727

28+
if (System.getProperty("os.name").toLowerCase().contains("window")) {
29+
onlyOnWindows();
30+
}
31+
2832
if (System.getProperty("os.name").toUpperCase().contains("WINDOWS")) {
2933
onlyOnWindows();
3034
}

java/ql/test/library-tests/os/specific-unix-variant-test.expected

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -27,11 +27,11 @@
2727
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1415:5:1415:84 | IS_OS_SOLARIS |
2828
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1427:5:1427:83 | IS_OS_SUN_OS |
2929
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1625:5:1625:80 | IS_OS_ZOS |
30-
| Test.java:103:13:103:73 | contains(...) |
31-
| Test.java:107:13:107:59 | contains(...) |
32-
| Test.java:111:13:111:35 | SystemUtils.IS_OS_LINUX |
33-
| Test.java:117:14:117:36 | SystemUtils.IS_OS_LINUX |
34-
| Test.java:125:13:125:62 | contains(...) |
35-
| Test.java:129:14:129:72 | contains(...) |
36-
| Test.java:133:14:133:34 | SystemUtils.IS_OS_MAC |
37-
| Test.java:139:14:139:45 | SystemUtils.IS_OS_MAC_OSX_MOJAVE |
30+
| Test.java:107:13:107:73 | contains(...) |
31+
| Test.java:111:13:111:59 | contains(...) |
32+
| Test.java:115:13:115:35 | SystemUtils.IS_OS_LINUX |
33+
| Test.java:121:14:121:36 | SystemUtils.IS_OS_LINUX |
34+
| Test.java:129:13:129:62 | contains(...) |
35+
| Test.java:133:14:133:72 | contains(...) |
36+
| Test.java:137:14:137:34 | SystemUtils.IS_OS_MAC |
37+
| Test.java:143:14:143:45 | SystemUtils.IS_OS_MAC_OSX_MOJAVE |

java/ql/test/library-tests/os/specific-windows-variant-test.expected

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,4 @@
1111
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1584:5:1584:86 | IS_OS_WINDOWS_7 |
1212
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1596:5:1596:86 | IS_OS_WINDOWS_8 |
1313
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1608:5:1608:87 | IS_OS_WINDOWS_10 |
14-
| Test.java:38:13:38:40 | SystemUtils.IS_OS_WINDOWS_XP |
14+
| Test.java:42:13:42:40 | SystemUtils.IS_OS_WINDOWS_XP |
Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1439:5:1439:81 | IS_OS_UNIX |
2-
| Test.java:66:13:66:95 | contains(...) |
3-
| Test.java:70:13:70:84 | contains(...) |
4-
| Test.java:74:13:74:34 | SystemUtils.IS_OS_UNIX |
5-
| Test.java:81:13:81:41 | ... == ... |
6-
| Test.java:85:13:85:37 | ... == ... |
2+
| Test.java:70:13:70:95 | contains(...) |
3+
| Test.java:74:13:74:84 | contains(...) |
4+
| Test.java:78:13:78:34 | SystemUtils.IS_OS_UNIX |
5+
| Test.java:85:13:85:41 | ... == ... |
76
| Test.java:89:13:89:37 | ... == ... |
8-
| Test.java:93:13:93:33 | ... == ... |
9-
| Test.java:97:13:97:60 | equals(...) |
7+
| Test.java:93:13:93:37 | ... == ... |
8+
| Test.java:97:13:97:33 | ... == ... |
9+
| Test.java:101:13:101:60 | equals(...) |
Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1451:5:1451:84 | IS_OS_WINDOWS |
22
| Test.java:20:13:20:61 | contains(...) |
33
| Test.java:24:13:24:75 | contains(...) |
4-
| Test.java:28:13:28:75 | contains(...) |
5-
| Test.java:32:13:32:37 | SystemUtils.IS_OS_WINDOWS |
6-
| Test.java:44:13:44:41 | ... == ... |
7-
| Test.java:48:13:48:37 | ... == ... |
8-
| Test.java:52:13:52:38 | ... == ... |
9-
| Test.java:56:13:56:34 | ... == ... |
10-
| Test.java:60:13:60:60 | equals(...) |
4+
| Test.java:28:13:28:74 | contains(...) |
5+
| Test.java:32:13:32:75 | contains(...) |
6+
| Test.java:36:13:36:37 | SystemUtils.IS_OS_WINDOWS |
7+
| Test.java:48:13:48:41 | ... == ... |
8+
| Test.java:52:13:52:37 | ... == ... |
9+
| Test.java:56:13:56:38 | ... == ... |
10+
| Test.java:60:13:60:34 | ... == ... |
11+
| Test.java:64:13:64:60 | equals(...) |

0 commit comments

Comments
 (0)