-
Notifications
You must be signed in to change notification settings - Fork 2k
Java: Add Guard Classes for checking OS & unify System Property Access #8032
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
cd073a2
39828fd
3cdfc00
4951344
9f5022e
fd63107
5913c9a
dad9a02
82d3cd8
3c53a05
a7adbb7
85de9f3
fea5006
103c770
31527a6
7ab193d
5243fe3
523ddb7
b282c7f
5b651f2
a21992a
2a6c4e9
ecb8911
1c98642
50ff2c2
451661d
09cc8ee
b11340c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -67,6 +67,42 @@ private class IsWindowsFromSystemProp extends IsWindowsGuard instanceof MethodAc | |
| IsWindowsFromSystemProp() { isOsFromSystemProp(this, "window%") } | ||
| } | ||
|
|
||
| /** | ||
| * Holds when the Guard is an equality check between the Field `f` and the string or char constant `compareToLiteral`. | ||
| */ | ||
| private Guard isOsFromFieldEqualityCheck(Field f, string compareToLiteral) { | ||
| result | ||
| .isEquality(any(FieldAccess fa | fa.getField() = f), | ||
| any(Literal literal | | ||
| (literal instanceof CharacterLiteral or literal instanceof StringLiteral) and | ||
| literal.getValue() = compareToLiteral | ||
| ), _) | ||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @smowton I could probably simplify this logic with the following diff to diff --git a/java/ql/lib/semmle/code/java/Expr.qll b/java/ql/lib/semmle/code/java/Expr.qll
index 5a991d8814..d98cdbb6b0 100755
--- a/java/ql/lib/semmle/code/java/Expr.qll
+++ b/java/ql/lib/semmle/code/java/Expr.qll
@@ -168,6 +168,8 @@ class CompileTimeConstantExpr extends Expr {
string getStringValue() {
result = this.(StringLiteral).getValue()
or
+ result = this.(CharacterLiteral).getValue()
+ or
result =
this.(AddExpr).getLeftOperand().(CompileTimeConstantExpr).getStringValue() +
this.(AddExpr).getRightOperand().(CompileTimeConstantExpr).getStringValue()
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. To make it easier to review this PR which already has scope-creep, I broke this out into an independent PR: #8325
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Should probably consider the polarity (currently specified as
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This is my understanding of guards from someone who's recently been introduced to them. So, this is my lay-person's explanation: This predicate is is establishing that a guard exists, not whether or not the guard guarantees that a specific case is true or false. IE. Both of these are 'windows guards':
However, their end-use utility as guards is only in the larger context of code being guarded is only possible when they are used with TL;DR:
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Looks like my assessment here was kinda wrong. 🤔 |
||
| } | ||
|
|
||
| private class IsWindowsFromCharPathSeperator extends IsWindowsGuard { | ||
| IsWindowsFromCharPathSeperator() { | ||
| this = isOsFromFieldEqualityCheck(any(FieldFilePathSeparator f), ";") | ||
| } | ||
| } | ||
|
|
||
| private class IsWindowsFromCharSeperator extends IsWindowsGuard { | ||
| IsWindowsFromCharSeperator() { | ||
| this = isOsFromFieldEqualityCheck(any(FieldFileSeparator f), "\\") | ||
| } | ||
| } | ||
|
|
||
| private class IsUnixFromCharPathSeperator extends IsUnixGuard { | ||
| IsUnixFromCharPathSeperator() { | ||
| this = isOsFromFieldEqualityCheck(any(FieldFilePathSeparator f), ":") | ||
| } | ||
| } | ||
|
|
||
| private class IsUnixFromCharSeperator extends IsUnixGuard { | ||
| IsUnixFromCharSeperator() { | ||
| this = isOsFromFieldEqualityCheck(any(FieldFileSeparator f), "/") | ||
| } | ||
| } | ||
|
|
||
| private class IsUnixFromSystemProp extends IsAnyUnixGuard instanceof MethodAccess { | ||
| IsUnixFromSystemProp() { isOsFromSystemProp(this, ["mac%", "linux%"]) } | ||
|
JLLeitschuh marked this conversation as resolved.
Outdated
|
||
| } | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,4 +1,4 @@ | ||
| | ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1439:5:1439:81 | IS_OS_UNIX | | ||
| | Test.java:44:13:44:95 | contains(...) | | ||
| | Test.java:48:13:48:84 | contains(...) | | ||
| | Test.java:52:13:52:34 | SystemUtils.IS_OS_UNIX | | ||
| | Test.java:62:13:62:95 | contains(...) | | ||
| | Test.java:66:13:66:84 | contains(...) | | ||
| | Test.java:70:13:70:34 | SystemUtils.IS_OS_UNIX | |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| | ../../stubs/apache-commons-lang3-3.7/org/apache/commons/lang3/SystemUtils.java:1451:5:1451:84 | IS_OS_WINDOWS | | ||
| | Test.java:18:13:18:61 | contains(...) | | ||
| | Test.java:22:13:22:75 | contains(...) | | ||
| | Test.java:26:13:26:75 | contains(...) | | ||
| | Test.java:30:13:30:37 | SystemUtils.IS_OS_WINDOWS | | ||
| | Test.java:20:13:20:61 | contains(...) | | ||
| | Test.java:24:13:24:75 | contains(...) | | ||
| | Test.java:28:13:28:75 | contains(...) | | ||
| | Test.java:32:13:32:37 | SystemUtils.IS_OS_WINDOWS | |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe a bit difficult to make a clear distinction, but this might rather be
IsSpecificWindowsVariantdue to the%?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🤔 Technically, it would be a regular expression of
windows?since I want to support matching bothwindowandwindowsThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've updated the code to use a regular expression instead