Add OS Checks based upon separator or path separator

This commit is contained in:
Jonathan Leitschuh
2022-03-02 14:15:56 -05:00
parent 82d3cd8924
commit 3c53a05e16
8 changed files with 103 additions and 21 deletions

View File

@@ -202,6 +202,21 @@ class TypeFile extends Class {
TypeFile() { this.hasQualifiedName("java.io", "File") }
}
/** The field `java.io.File.separator` or `java.io.File.separatorChar` */
class FieldFileSeparator extends Field {
FieldFileSeparator() {
this.getDeclaringType() instanceof TypeFile and this.hasName(["separator", "separatorChar"])
}
}
/* The field `java.io.File.pathSeparator` or `java.io.File.pathSeparatorChar` */
class FieldFilePathSeparator extends Field {
FieldFilePathSeparator() {
this.getDeclaringType() instanceof TypeFile and
this.hasName(["pathSeparator", "pathSeparatorChar"])
}
}
// --- Standard methods ---
/**
* Any constructor of class `java.lang.ProcessBuilder`.

View File

@@ -325,13 +325,10 @@ private predicate formatStringValue(Expr e, string fmtvalue) {
or
exists(Field f |
e = f.getAnAccess() and
f.getDeclaringType() instanceof TypeFile and
fmtvalue = "x" // dummy value
|
f.hasName("pathSeparator") or
f.hasName("pathSeparatorChar") or
f.hasName("separator") or
f.hasName("separatorChar")
f instanceof FieldFileSeparator or
f instanceof FieldFilePathSeperator
)
)
}

View File

@@ -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
), _)
}
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%"]) }
}