diff --git a/java/change-notes/2021-10-29-deprecate-String-getRepresentedString.md b/java/change-notes/2021-10-29-deprecate-String-getRepresentedString.md new file mode 100644 index 000000000000..d7ecd9b3b966 --- /dev/null +++ b/java/change-notes/2021-10-29-deprecate-String-getRepresentedString.md @@ -0,0 +1,2 @@ +lgtm,codescanning +* The predicate `StringLiteral.getRepresentedString()` has been deprecated for removal in a future version because it is just an alias for `getValue()`. That predicate should be used instead. diff --git a/java/ql/lib/semmle/code/java/Expr.qll b/java/ql/lib/semmle/code/java/Expr.qll index 2d6fc9c16712..8aad29dec092 100755 --- a/java/ql/lib/semmle/code/java/Expr.qll +++ b/java/ql/lib/semmle/code/java/Expr.qll @@ -166,7 +166,7 @@ class CompileTimeConstantExpr extends Expr { */ pragma[nomagic] string getStringValue() { - result = this.(StringLiteral).getRepresentedString() + result = this.(StringLiteral).getValue() or result = this.(AddExpr).getLeftOperand().(CompileTimeConstantExpr).getStringValue() + @@ -745,9 +745,21 @@ class CharacterLiteral extends Literal, @characterliteral { */ class StringLiteral extends Literal, @stringliteral { /** + * Gets the string represented by this string literal, that is, the content + * of the literal without enclosing quotes and with escape sequences translated. + * + * Unpaired Unicode surrogate characters (U+D800 to U+DFFF) are replaced with the + * replacement character U+FFFD. + */ + override string getValue() { result = super.getValue() } + + /** + * DEPRECATED: This predicate will be removed in a future version because + * it is just an alias for `getValue()`; that predicate should be used instead. + * * Gets the literal string without the quotes. */ - string getRepresentedString() { result = this.getValue() } + deprecated string getRepresentedString() { result = this.getValue() } /** Holds if this string literal is a text block (`""" ... """`). */ predicate isTextBlock() { this.getLiteral().matches("\"\"\"%") } diff --git a/java/ql/lib/semmle/code/java/JDKAnnotations.qll b/java/ql/lib/semmle/code/java/JDKAnnotations.qll index 0b56599caa29..2dff70c4d8ee 100644 --- a/java/ql/lib/semmle/code/java/JDKAnnotations.qll +++ b/java/ql/lib/semmle/code/java/JDKAnnotations.qll @@ -25,9 +25,7 @@ class SuppressWarningsAnnotation extends Annotation { } /** Gets the name of a warning suppressed by this annotation. */ - string getASuppressedWarning() { - result = this.getASuppressedWarningLiteral().getRepresentedString() - } + string getASuppressedWarning() { result = this.getASuppressedWarningLiteral().getValue() } } /** A `@Target` annotation. */ diff --git a/java/ql/lib/semmle/code/java/Reflection.qll b/java/ql/lib/semmle/code/java/Reflection.qll index 71864c5cfe9e..cd1c9f59f0cc 100644 --- a/java/ql/lib/semmle/code/java/Reflection.qll +++ b/java/ql/lib/semmle/code/java/Reflection.qll @@ -75,7 +75,7 @@ class ReflectiveClassIdentifierMethodAccess extends ReflectiveClassIdentifier, M /** * If the argument to this call is a `StringLiteral`, then return that string. */ - string getTypeName() { result = this.getArgument(0).(StringLiteral).getRepresentedString() } + string getTypeName() { result = this.getArgument(0).(StringLiteral).getValue() } override RefType getReflectivelyIdentifiedClass() { // We only handle cases where the class is specified as a string literal to this call. @@ -360,7 +360,7 @@ class ReflectiveMethodAccess extends ClassMethodAccess { this.getInferredClassType().inherits(result) ) and // Only consider instances where the method name is provided as a `StringLiteral`. - result.hasName(this.getArgument(0).(StringLiteral).getRepresentedString()) + result.hasName(this.getArgument(0).(StringLiteral).getValue()) } } @@ -400,6 +400,6 @@ class ReflectiveFieldAccess extends ClassMethodAccess { this.getInferredClassType().inherits(result) ) ) and - result.hasName(this.getArgument(0).(StringLiteral).getRepresentedString()) + result.hasName(this.getArgument(0).(StringLiteral).getValue()) } } diff --git a/java/ql/lib/semmle/code/java/StringFormat.qll b/java/ql/lib/semmle/code/java/StringFormat.qll index c6f9a7814dbc..2938f5255fad 100644 --- a/java/ql/lib/semmle/code/java/StringFormat.qll +++ b/java/ql/lib/semmle/code/java/StringFormat.qll @@ -279,7 +279,7 @@ private predicate formatStringFragment(Expr fmt) { private predicate formatStringValue(Expr e, string fmtvalue) { formatStringFragment(e) and ( - e.(StringLiteral).getRepresentedString() = fmtvalue + e.(StringLiteral).getValue() = fmtvalue or e.getType() instanceof IntegralType and fmtvalue = "1" // dummy value or @@ -318,7 +318,7 @@ private predicate formatStringValue(Expr e, string fmtvalue) { getprop.hasName("getProperty") and getprop.getDeclaringType().hasQualifiedName("java.lang", "System") and getprop.getNumberOfParameters() = 1 and - ma.getAnArgument().(StringLiteral).getRepresentedString() = prop and + ma.getAnArgument().(StringLiteral).getValue() = prop and (prop = "line.separator" or prop = "file.separator" or prop = "path.separator") and fmtvalue = "x" // dummy value ) diff --git a/java/ql/lib/semmle/code/java/UnitTests.qll b/java/ql/lib/semmle/code/java/UnitTests.qll index 6115094e5d7d..e56b9a6dc23d 100644 --- a/java/ql/lib/semmle/code/java/UnitTests.qll +++ b/java/ql/lib/semmle/code/java/UnitTests.qll @@ -162,7 +162,7 @@ class TestNGTestMethod extends Method { testAnnotation = this.getAnAnnotation() and // The data provider must have the same name as the referenced data provider result.getDataProviderName() = - testAnnotation.getValue("dataProvider").(StringLiteral).getRepresentedString() + testAnnotation.getValue("dataProvider").(StringLiteral).getValue() | // Either the data provider should be on the current class, or a supertype this.getDeclaringType().getAnAncestor() = result.getDeclaringType() @@ -258,7 +258,7 @@ class TestNGDataProviderMethod extends Method { .(TestNGDataProviderAnnotation) .getValue("name") .(StringLiteral) - .getRepresentedString() + .getValue() } } diff --git a/java/ql/lib/semmle/code/java/dataflow/internal/TaintTrackingUtil.qll b/java/ql/lib/semmle/code/java/dataflow/internal/TaintTrackingUtil.qll index e4bfaaae1cc3..4e742238209f 100644 --- a/java/ql/lib/semmle/code/java/dataflow/internal/TaintTrackingUtil.qll +++ b/java/ql/lib/semmle/code/java/dataflow/internal/TaintTrackingUtil.qll @@ -300,8 +300,8 @@ private predicate unsafeEscape(MethodAccess ma) { // Removing `