Skip to content

Commit 5913c9a

Browse files
committed
Refactor OS Guard Checks
1 parent fd63107 commit 5913c9a

File tree

8 files changed

+175
-39
lines changed

8 files changed

+175
-39
lines changed

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

Lines changed: 38 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,33 @@ private import semmle.code.java.frameworks.apache.Lang
88
private import semmle.code.java.dataflow.DataFlow
99

1010
/**
11-
* A guard that checks if the current platform is Windows.
11+
* A guard that checks if the current os is Windows.
12+
* When True, the OS is Windows.
13+
* When False, the OS is not Windows.
1214
*/
1315
abstract class IsWindowsGuard extends Guard { }
1416

1517
/**
16-
* A guard that checks if the current platform is unix or unix-like.
18+
* A guard that checks if the current OS is any Windows.
19+
* When True, the OS is Windows.
20+
* When False, the OS *may* still be Windows.
21+
*/
22+
abstract class IsAnyWindowsGuard extends Guard { }
23+
24+
/**
25+
* A guard that checks if the current OS is unix or unix-like.
26+
* When True, the OS is unix or unix-like.
27+
* When False, the OS is not unix or unix-like.
1728
*/
1829
abstract class IsUnixGuard extends Guard { }
1930

31+
/**
32+
* A guard that checks if the current OS is unix or unix-like.
33+
* When True, the OS is unix or unix-like.
34+
* When False, the OS *may* still be unix or unix-like.
35+
*/
36+
abstract class IsAnyUnixGuard extends Guard { }
37+
2038
/**
2139
* Holds when the MethodAccess is a call to check the current OS using either the upper case `osUpperCase` or lower case `osLowerCase` string constants.
2240
*/
@@ -48,7 +66,7 @@ private class IsWindowsFromSystemProp extends IsWindowsGuard instanceof MethodAc
4866
IsWindowsFromSystemProp() { isOsFromSystemProp(this, "window%") }
4967
}
5068

51-
private class IsUnixFromSystemProp extends IsUnixGuard instanceof MethodAccess {
69+
private class IsUnixFromSystemProp extends IsAnyUnixGuard instanceof MethodAccess {
5270
IsUnixFromSystemProp() { isOsFromSystemProp(this, ["mac%", "linux%"]) }
5371
}
5472

@@ -61,16 +79,31 @@ private predicate isOsFromApacheCommons(FieldAccess fa, string fieldNamePattern)
6179
}
6280

6381
private class IsWindowsFromApacheCommons extends IsWindowsGuard instanceof FieldAccess {
64-
IsWindowsFromApacheCommons() { isOsFromApacheCommons(this, "IS_OS_WINDOWS%") }
82+
IsWindowsFromApacheCommons() { isOsFromApacheCommons(this, "IS_OS_WINDOWS") }
83+
}
84+
85+
private class IsAnyWindowsFromApacheCommons extends IsAnyWindowsGuard instanceof FieldAccess {
86+
IsAnyWindowsFromApacheCommons() { isOsFromApacheCommons(this, "IS_OS_WINDOWS_%") }
6587
}
6688

6789
private class IsUnixFromApacheCommons extends IsUnixGuard instanceof FieldAccess {
6890
IsUnixFromApacheCommons() { isOsFromApacheCommons(this, "IS_OS_UNIX") }
6991
}
7092

93+
private class IsAnyUnixFromApacheCommons extends IsAnyUnixGuard instanceof FieldAccess {
94+
IsAnyUnixFromApacheCommons() {
95+
isOsFromApacheCommons(this,
96+
[
97+
"IS_OS_AIX", "IS_OS_HP_UX", "IS_OS_IRIX", "IS_OS_LINUX", "IS_OS_MAC%", "IS_OS_FREE_BSD",
98+
"IS_OS_OPEN_BSD", "IS_OS_NET_BSD", "IS_OS_SOLARIS", "IS_OS_SUN_OS", "IS_OS_ZOS"
99+
])
100+
}
101+
}
102+
71103
/**
72104
* A guard that checks if the `java.nio.file.FileSystem` supports posix file permissions.
73-
* This is often used to infer if the OS is unix-based.
105+
* This is often used to infer if the OS is unix-based and can generally be considered to be true for all unix-based OSes
106+
* ([source](https://en.wikipedia.org/wiki/POSIX#POSIX-oriented_operating_systems)).
74107
* Looks for calls to `contains("posix")` on the `supportedFileAttributeViews()` method returned by `FileSystem`.
75108
*/
76109
private class IsUnixFromPosixFromFileSystem extends IsUnixGuard instanceof MethodAccess {

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

Lines changed: 69 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,41 +4,98 @@
44
import org.apache.commons.lang3.SystemUtils;
55

66
public class Test {
7-
void test() {
7+
/**
8+
* Should only be called on windows
9+
*/
10+
private void onlyOnWindows() {}
11+
12+
/**
13+
* Should only be called on unix-like systems
14+
*/
15+
private void onlyOnUnix() {}
16+
17+
void testWindows() {
818
if (System.getProperty("os.name").contains("Windows")) {
9-
19+
onlyOnWindows();
1020
}
1121

1222
if (System.getProperty("os.name").toLowerCase().contains("windows")) {
13-
23+
onlyOnWindows();
1424
}
1525

16-
if (System.getProperty("os.name").contains("Linux")) {
17-
26+
if (System.getProperty("os.name").toUpperCase().contains("WINDOWS")) {
27+
onlyOnWindows();
1828
}
1929

20-
if (System.getProperty("os.name").contains("Mac OS X")) {
21-
30+
if (SystemUtils.IS_OS_WINDOWS) {
31+
onlyOnWindows();
32+
} else {
33+
onlyOnUnix();
2234
}
2335

24-
if (System.getProperty("os.name").toLowerCase().contains("mac")) {
25-
36+
if (SystemUtils.IS_OS_WINDOWS_XP) {
37+
onlyOnWindows();
38+
} else {
39+
// Might be another version of windows
2640
}
41+
}
2742

43+
void testUnix() {
2844
if (Path.of("whatever").getFileSystem().supportedFileAttributeViews().contains("posix")) {
29-
45+
onlyOnUnix();
3046
}
3147

3248
if (FileSystems.getDefault().supportedFileAttributeViews().contains("posix")) {
49+
onlyOnUnix();
50+
}
3351

52+
if (SystemUtils.IS_OS_UNIX) {
53+
onlyOnUnix();
54+
} else {
55+
// Reasonable assumption, maybe not 100% accurate, but it's 'good enough'
56+
onlyOnWindows();
3457
}
58+
}
3559

36-
if (SystemUtils.IS_OS_WINDOWS) {
60+
void testLinux() {
61+
if (System.getProperty("os.name").toLowerCase().contains("linux")) {
62+
onlyOnUnix();
63+
}
3764

65+
if (System.getProperty("os.name").contains("Linux")) {
66+
onlyOnUnix();
3867
}
3968

40-
if (SystemUtils.IS_OS_UNIX) {
69+
if (SystemUtils.IS_OS_LINUX) {
70+
onlyOnUnix();
71+
} else {
72+
// Might be another different unix-like system, so this can't be `onlyOnWindows()`.
73+
}
74+
75+
if (!SystemUtils.IS_OS_LINUX) {
76+
// Might be another different unix-like system, so this can't be `onlyOnWindows()`.
77+
} else {
78+
onlyOnUnix();
79+
}
80+
}
81+
82+
void testMacOs() {
83+
if (System.getProperty("os.name").contains("Mac OS X")) {
84+
onlyOnUnix();
85+
}
4186

87+
if (System.getProperty("os.name").toLowerCase().contains("mac")) {
88+
onlyOnUnix();
4289
}
90+
91+
if (SystemUtils.IS_OS_MAC) {
92+
onlyOnUnix();
93+
} else {
94+
// Can't assume this is windows, it could be another unix-like OS
95+
}
96+
97+
if (SystemUtils.IS_OS_MAC_OSX_MOJAVE) {
98+
onlyOnUnix();
99+
}
43100
}
44101
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1079:5:1079:80 | IS_OS_AIX |
2+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1091:5:1091:82 | IS_OS_HP_UX |
3+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1115:5:1115:81 | IS_OS_IRIX |
4+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1127:5:1127:82 | IS_OS_LINUX |
5+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1139:5:1139:80 | IS_OS_MAC |
6+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1151:5:1151:84 | IS_OS_MAC_OSX |
7+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1163:5:1163:92 | IS_OS_MAC_OSX_CHEETAH |
8+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1175:5:1175:89 | IS_OS_MAC_OSX_PUMA |
9+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1187:5:1187:91 | IS_OS_MAC_OSX_JAGUAR |
10+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1199:5:1199:92 | IS_OS_MAC_OSX_PANTHER |
11+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1211:5:1211:90 | IS_OS_MAC_OSX_TIGER |
12+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1223:5:1223:92 | IS_OS_MAC_OSX_LEOPARD |
13+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1235:5:1235:97 | IS_OS_MAC_OSX_SNOW_LEOPARD |
14+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1247:5:1247:89 | IS_OS_MAC_OSX_LION |
15+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1259:5:1259:98 | IS_OS_MAC_OSX_MOUNTAIN_LION |
16+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1271:5:1271:94 | IS_OS_MAC_OSX_MAVERICKS |
17+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1283:5:1283:93 | IS_OS_MAC_OSX_YOSEMITE |
18+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1295:5:1295:95 | IS_OS_MAC_OSX_EL_CAPITAN |
19+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1307:5:1307:91 | IS_OS_MAC_OSX_SIERRA |
20+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1319:5:1319:96 | IS_OS_MAC_OSX_HIGH_SIERRA |
21+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1331:5:1331:91 | IS_OS_MAC_OSX_MOJAVE |
22+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1343:5:1343:93 | IS_OS_MAC_OSX_CATALINA |
23+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1355:5:1355:92 | IS_OS_MAC_OSX_BIG_SUR |
24+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1367:5:1367:85 | IS_OS_FREE_BSD |
25+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1379:5:1379:85 | IS_OS_OPEN_BSD |
26+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1391:5:1391:84 | IS_OS_NET_BSD |
27+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1415:5:1415:84 | IS_OS_SOLARIS |
28+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1427:5:1427:83 | IS_OS_SUN_OS |
29+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1625:5:1625:80 | IS_OS_ZOS |
30+
| Test.java:61:13:61:73 | contains(...) |
31+
| Test.java:65:13:65:59 | contains(...) |
32+
| Test.java:69:13:69:35 | SystemUtils.IS_OS_LINUX |
33+
| Test.java:75:14:75:36 | SystemUtils.IS_OS_LINUX |
34+
| Test.java:83:13:83:62 | contains(...) |
35+
| Test.java:87:14:87:72 | contains(...) |
36+
| Test.java:91:14:91:34 | SystemUtils.IS_OS_MAC |
37+
| Test.java:97:14:97:45 | SystemUtils.IS_OS_MAC_OSX_MOJAVE |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import default
2+
import semmle.code.java.os.OSCheck
3+
4+
from IsAnyUnixGuard isAnyUnix
5+
select isAnyUnix
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1463:5:1463:89 | IS_OS_WINDOWS_2000 |
2+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1475:5:1475:89 | IS_OS_WINDOWS_2003 |
3+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1487:5:1487:89 | IS_OS_WINDOWS_2008 |
4+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1499:5:1499:89 | IS_OS_WINDOWS_2012 |
5+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1511:5:1511:87 | IS_OS_WINDOWS_95 |
6+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1523:5:1523:87 | IS_OS_WINDOWS_98 |
7+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1535:5:1535:87 | IS_OS_WINDOWS_ME |
8+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1547:5:1547:87 | IS_OS_WINDOWS_NT |
9+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1559:5:1559:87 | IS_OS_WINDOWS_XP |
10+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1572:5:1572:90 | IS_OS_WINDOWS_VISTA |
11+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1584:5:1584:86 | IS_OS_WINDOWS_7 |
12+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1596:5:1596:86 | IS_OS_WINDOWS_8 |
13+
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1608:5:1608:87 | IS_OS_WINDOWS_10 |
14+
| Test.java:36:13:36:40 | SystemUtils.IS_OS_WINDOWS_XP |
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import default
2+
import semmle.code.java.os.OSCheck
3+
4+
from IsAnyWindowsGuard isAnyWindows
5+
select isAnyWindows
Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,4 @@
11
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1439:5:1439:81 | IS_OS_UNIX |
2-
| Test.java:16:13:16:59 | contains(...) |
3-
| Test.java:20:13:20:62 | contains(...) |
4-
| Test.java:24:13:24:71 | contains(...) |
5-
| Test.java:28:13:28:95 | contains(...) |
6-
| Test.java:32:13:32:84 | contains(...) |
7-
| Test.java:40:13:40:34 | SystemUtils.IS_OS_UNIX |
2+
| Test.java:44:13:44:95 | contains(...) |
3+
| Test.java:48:13:48:84 | contains(...) |
4+
| Test.java:52:13:52:34 | SystemUtils.IS_OS_UNIX |
Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,5 @@
11
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1451:5:1451:84 | IS_OS_WINDOWS |
2-
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1463:5:1463:89 | IS_OS_WINDOWS_2000 |
3-
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1475:5:1475:89 | IS_OS_WINDOWS_2003 |
4-
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1487:5:1487:89 | IS_OS_WINDOWS_2008 |
5-
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1499:5:1499:89 | IS_OS_WINDOWS_2012 |
6-
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1511:5:1511:87 | IS_OS_WINDOWS_95 |
7-
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1523:5:1523:87 | IS_OS_WINDOWS_98 |
8-
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1535:5:1535:87 | IS_OS_WINDOWS_ME |
9-
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1547:5:1547:87 | IS_OS_WINDOWS_NT |
10-
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1559:5:1559:87 | IS_OS_WINDOWS_XP |
11-
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1572:5:1572:90 | IS_OS_WINDOWS_VISTA |
12-
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1584:5:1584:86 | IS_OS_WINDOWS_7 |
13-
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1596:5:1596:86 | IS_OS_WINDOWS_8 |
14-
| ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1608:5:1608:87 | IS_OS_WINDOWS_10 |
15-
| Test.java:8:13:8:61 | contains(...) |
16-
| Test.java:12:13:12:75 | contains(...) |
17-
| Test.java:36:13:36:37 | SystemUtils.IS_OS_WINDOWS |
2+
| Test.java:18:13:18:61 | contains(...) |
3+
| Test.java:22:13:22:75 | contains(...) |
4+
| Test.java:26:13:26:75 | contains(...) |
5+
| Test.java:30:13:30:37 | SystemUtils.IS_OS_WINDOWS |

0 commit comments

Comments
 (0)