Skip to content

Commit cd059eb

Browse files
committed
Java: Add StringBuildingType
1 parent 07ca09e commit cd059eb

File tree

6 files changed

+10
-18
lines changed

6 files changed

+10
-18
lines changed

java/ql/src/Likely Bugs/Likely Typos/StringBufferCharInit.ql

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,7 @@
1313
import java
1414

1515
class NewStringBufferOrBuilder extends ClassInstanceExpr {
16-
NewStringBufferOrBuilder() {
17-
exists(Class c | c = this.getConstructedType() |
18-
c.hasQualifiedName("java.lang", "StringBuilder") or
19-
c.hasQualifiedName("java.lang", "StringBuffer")
20-
)
21-
}
16+
NewStringBufferOrBuilder() { getConstructedType() instanceof StringBuildingType }
2217

2318
string getName() { result = this.getConstructedType().getName() }
2419
}

java/ql/src/Security/CWE/CWE-327/MaybeBrokenCryptoAlgorithm.ql

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,8 +44,7 @@ predicate objectToString(MethodAccess ma) {
4444
class StringContainer extends RefType {
4545
StringContainer() {
4646
this instanceof TypeString or
47-
this.hasQualifiedName("java.lang", "StringBuilder") or
48-
this.hasQualifiedName("java.lang", "StringBuffer") or
47+
this instanceof StringBuildingType or
4948
this.hasQualifiedName("java.util", "StringTokenizer") or
5049
this.(Array).getComponentType() instanceof StringContainer
5150
}

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

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ class TypeStringBuilder extends Class {
4646
TypeStringBuilder() { this.hasQualifiedName("java.lang", "StringBuilder") }
4747
}
4848

49+
/** Class `java.lang.StringBuffer` or `java.lang.StringBuilder`. */
50+
class StringBuildingType extends Class {
51+
StringBuildingType() { this instanceof TypeStringBuffer or this instanceof TypeStringBuilder }
52+
}
53+
4954
/** The class `java.lang.System`. */
5055
class TypeSystem extends Class {
5156
TypeSystem() { this.hasQualifiedName("java.lang", "System") }

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

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -210,10 +210,7 @@ private predicate printMethod(Method m, int i) {
210210
(t.hasQualifiedName("java.io", "PrintWriter") or t.hasQualifiedName("java.io", "PrintStream")) and
211211
(m.hasName("print") or m.hasName("println"))
212212
or
213-
(
214-
t.hasQualifiedName("java.lang", "StringBuilder") or
215-
t.hasQualifiedName("java.lang", "StringBuffer")
216-
) and
213+
t instanceof StringBuildingType and
217214
(m.hasName("append") or m.hasName("insert"))
218215
or
219216
t instanceof TypeString and m.hasName("valueOf")

java/ql/src/semmle/code/java/dataflow/FlowSteps.qll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -152,8 +152,7 @@ private class NumberTaintPreservingCallable extends TaintPreservingCallable {
152152

153153
/** Holds for the types `StringBuilder`, `StringBuffer`, and `StringWriter`. */
154154
private predicate stringBuilderType(RefType t) {
155-
t.hasQualifiedName("java.lang", "StringBuilder") or
156-
t.hasQualifiedName("java.lang", "StringBuffer") or
155+
t instanceof StringBuildingType or
157156
t.hasQualifiedName("java.io", "StringWriter")
158157
}
159158

java/ql/src/semmle/code/java/dataflow/internal/TaintTrackingUtil.qll

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -552,10 +552,7 @@ module StringBuilderVarModule {
552552
* build up a query using string concatenation.
553553
*/
554554
class StringBuilderVar extends LocalVariableDecl {
555-
StringBuilderVar() {
556-
this.getType() instanceof TypeStringBuilder or
557-
this.getType() instanceof TypeStringBuffer
558-
}
555+
StringBuilderVar() { getType() instanceof StringBuildingType }
559556

560557
/**
561558
* Gets a call that adds something to this string builder, from the argument at the given index.

0 commit comments

Comments
 (0)