From 04cd0dbfe91aee7b0e83dfe50209b0df46262e87 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Thu, 3 Mar 2022 17:49:43 -0500 Subject: [PATCH 1/3] [Java] Add CharacterLiteral to CompileTimeConstantExpr.getStringValue --- ...3-03-char-literal-in-compile-time-constant.md | 4 ++++ java/ql/lib/semmle/code/java/Expr.qll | 2 ++ .../constants/CompileTimeConstantExpr.expected | 1 + .../library-tests/constants/PrintAst.expected | 16 +++++++++++++++- .../constants/constants/Constants.java | 1 + .../constants/constants/Values.java | 2 ++ .../constants/getStringValue.expected | 3 +++ .../library-tests/constants/getStringValue.ql | 9 +++++++++ 8 files changed, 37 insertions(+), 1 deletion(-) create mode 100644 java/ql/lib/change-notes/2022-03-03-char-literal-in-compile-time-constant.md create mode 100644 java/ql/test/library-tests/constants/getStringValue.expected create mode 100644 java/ql/test/library-tests/constants/getStringValue.ql diff --git a/java/ql/lib/change-notes/2022-03-03-char-literal-in-compile-time-constant.md b/java/ql/lib/change-notes/2022-03-03-char-literal-in-compile-time-constant.md new file mode 100644 index 000000000000..1ee68add8b14 --- /dev/null +++ b/java/ql/lib/change-notes/2022-03-03-char-literal-in-compile-time-constant.md @@ -0,0 +1,4 @@ +--- +category: minorAnalysis +--- + * Add support for `CharacterLiteral` in `CompileTimeConstantExpr.getStringValue()` diff --git a/java/ql/lib/semmle/code/java/Expr.qll b/java/ql/lib/semmle/code/java/Expr.qll index 5a991d8814c6..d98cdbb6b0e6 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() diff --git a/java/ql/test/library-tests/constants/CompileTimeConstantExpr.expected b/java/ql/test/library-tests/constants/CompileTimeConstantExpr.expected index f5e508b4a3c3..21331c4c32d5 100644 --- a/java/ql/test/library-tests/constants/CompileTimeConstantExpr.expected +++ b/java/ql/test/library-tests/constants/CompileTimeConstantExpr.expected @@ -15,3 +15,4 @@ | constants/Constants.java:20:22:20:22 | 5 | | constants/Constants.java:20:27:20:27 | 1 | | constants/Constants.java:20:31:20:31 | 2 | +| constants/Constants.java:21:22:21:24 | 'a' | diff --git a/java/ql/test/library-tests/constants/PrintAst.expected b/java/ql/test/library-tests/constants/PrintAst.expected index 241d837ea3cf..7e8148fc11ae 100644 --- a/java/ql/test/library-tests/constants/PrintAst.expected +++ b/java/ql/test/library-tests/constants/PrintAst.expected @@ -66,7 +66,11 @@ constants/Constants.java: # 20| 1: [IntegerLiteral] 5 # 20| 1: [IntegerLiteral] 1 # 20| 2: [IntegerLiteral] 2 -# 22| 11: [ReturnStmt] return ... +# 21| 11: [LocalVariableDeclStmt] var ...; +# 21| 0: [TypeAccess] char +# 21| 1: [LocalVariableDeclExpr] charLiteral +# 21| 0: [CharacterLiteral] 'a' +# 23| 12: [ReturnStmt] return ... constants/Initializers.java: # 0| [CompilationUnit] Initializers # 3| 1: [Class] Initializers @@ -512,3 +516,13 @@ constants/Values.java: # 90| 0: [TypeAccess] int # 90| 1: [LocalVariableDeclExpr] var_nonfinald_local # 90| 0: [VarAccess] var_field +# 91| 68: [LocalVariableDeclStmt] var ...; +# 91| 0: [TypeAccess] String +# 91| 1: [LocalVariableDeclExpr] concatinatedString +# 91| 0: [StringLiteral] "a" + "b" +# 92| 69: [LocalVariableDeclStmt] var ...; +# 92| 0: [TypeAccess] String +# 92| 1: [LocalVariableDeclExpr] concatinatedChar +# 92| 0: [AddExpr] ... + ... +# 92| 0: [StringLiteral] "ab" +# 92| 1: [CharacterLiteral] 'c' diff --git a/java/ql/test/library-tests/constants/constants/Constants.java b/java/ql/test/library-tests/constants/constants/Constants.java index fb0a2c910918..af4ae6c2466c 100644 --- a/java/ql/test/library-tests/constants/constants/Constants.java +++ b/java/ql/test/library-tests/constants/constants/Constants.java @@ -18,6 +18,7 @@ void constants(final int notConstant) { int paren = (12); String string = "a string"; int ternary = (3 < 5) ? 1 : 2; + char charLiteral = 'a'; return; } diff --git a/java/ql/test/library-tests/constants/constants/Values.java b/java/ql/test/library-tests/constants/constants/Values.java index 7cf88fb9ad82..a5a0530591dd 100644 --- a/java/ql/test/library-tests/constants/constants/Values.java +++ b/java/ql/test/library-tests/constants/constants/Values.java @@ -88,5 +88,7 @@ void values(final int notConstant) { int var_local = final_local; //42 int var_param = notConstant; //Not constant int var_nonfinald_local = var_field; //Not constant + String concatinatedString = "a" + "b"; //ab + String concatinatedChar = "ab" + 'c'; //abc } } diff --git a/java/ql/test/library-tests/constants/getStringValue.expected b/java/ql/test/library-tests/constants/getStringValue.expected new file mode 100644 index 000000000000..249fb1c16252 --- /dev/null +++ b/java/ql/test/library-tests/constants/getStringValue.expected @@ -0,0 +1,3 @@ +| constants/Values.java:19:29:19:31 | '*' | * | +| constants/Values.java:91:37:91:45 | "a" + "b" | ab | +| constants/Values.java:92:29:92:38 | ... + ... | abc | diff --git a/java/ql/test/library-tests/constants/getStringValue.ql b/java/ql/test/library-tests/constants/getStringValue.ql new file mode 100644 index 000000000000..6a6cc85bdfb5 --- /dev/null +++ b/java/ql/test/library-tests/constants/getStringValue.ql @@ -0,0 +1,9 @@ +import semmle.code.java.Variable + +from Variable v, Expr init, RefType enclosing, string constant +where + v.getInitializer() = init and + init.getEnclosingCallable().getDeclaringType() = enclosing and + enclosing.hasQualifiedName("constants", "Values") and + constant = init.(CompileTimeConstantExpr).getStringValue() +select init, constant From 17b6e66814f7f6d39f02c8c353af1a8ffdea156b Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Fri, 4 Mar 2022 09:29:57 -0500 Subject: [PATCH 2/3] Apply suggestions from code review Co-authored-by: Tony Torralba --- java/ql/test/library-tests/constants/constants/Values.java | 4 ++-- java/ql/test/library-tests/constants/getStringValue.ql | 4 ++-- 2 files changed, 4 insertions(+), 4 deletions(-) diff --git a/java/ql/test/library-tests/constants/constants/Values.java b/java/ql/test/library-tests/constants/constants/Values.java index a5a0530591dd..b59672f96e75 100644 --- a/java/ql/test/library-tests/constants/constants/Values.java +++ b/java/ql/test/library-tests/constants/constants/Values.java @@ -88,7 +88,7 @@ void values(final int notConstant) { int var_local = final_local; //42 int var_param = notConstant; //Not constant int var_nonfinald_local = var_field; //Not constant - String concatinatedString = "a" + "b"; //ab - String concatinatedChar = "ab" + 'c'; //abc + String concatenatedString = "a" + "b"; //ab + String concatenatedChar = "ab" + 'c'; //abc } } diff --git a/java/ql/test/library-tests/constants/getStringValue.ql b/java/ql/test/library-tests/constants/getStringValue.ql index 6a6cc85bdfb5..bbf7cd8b51e9 100644 --- a/java/ql/test/library-tests/constants/getStringValue.ql +++ b/java/ql/test/library-tests/constants/getStringValue.ql @@ -1,9 +1,9 @@ import semmle.code.java.Variable -from Variable v, Expr init, RefType enclosing, string constant +from Variable v, CompileTimeConstantExpr init, RefType enclosing, string constant where v.getInitializer() = init and init.getEnclosingCallable().getDeclaringType() = enclosing and enclosing.hasQualifiedName("constants", "Values") and - constant = init.(CompileTimeConstantExpr).getStringValue() + constant = init.getStringValue() select init, constant From 38897f2ec18d4949a49a2ac0a5983041d04e40d3 Mon Sep 17 00:00:00 2001 From: Jonathan Leitschuh Date: Fri, 4 Mar 2022 09:33:51 -0500 Subject: [PATCH 3/3] Fixup tests from code review changes --- java/ql/test/library-tests/constants/PrintAst.expected | 4 ++-- java/ql/test/library-tests/constants/getBooleanValue.ql | 4 ++-- java/ql/test/library-tests/constants/getIntValue.ql | 4 ++-- java/ql/test/library-tests/constants/getStringValue.expected | 2 +- 4 files changed, 7 insertions(+), 7 deletions(-) diff --git a/java/ql/test/library-tests/constants/PrintAst.expected b/java/ql/test/library-tests/constants/PrintAst.expected index 7e8148fc11ae..2ecc471d9811 100644 --- a/java/ql/test/library-tests/constants/PrintAst.expected +++ b/java/ql/test/library-tests/constants/PrintAst.expected @@ -518,11 +518,11 @@ constants/Values.java: # 90| 0: [VarAccess] var_field # 91| 68: [LocalVariableDeclStmt] var ...; # 91| 0: [TypeAccess] String -# 91| 1: [LocalVariableDeclExpr] concatinatedString +# 91| 1: [LocalVariableDeclExpr] concatenatedString # 91| 0: [StringLiteral] "a" + "b" # 92| 69: [LocalVariableDeclStmt] var ...; # 92| 0: [TypeAccess] String -# 92| 1: [LocalVariableDeclExpr] concatinatedChar +# 92| 1: [LocalVariableDeclExpr] concatenatedChar # 92| 0: [AddExpr] ... + ... # 92| 0: [StringLiteral] "ab" # 92| 1: [CharacterLiteral] 'c' diff --git a/java/ql/test/library-tests/constants/getBooleanValue.ql b/java/ql/test/library-tests/constants/getBooleanValue.ql index 2b10d95ff9be..460af42d3c30 100644 --- a/java/ql/test/library-tests/constants/getBooleanValue.ql +++ b/java/ql/test/library-tests/constants/getBooleanValue.ql @@ -1,9 +1,9 @@ import semmle.code.java.Variable -from Variable v, Expr init, RefType enclosing, boolean constant +from Variable v, CompileTimeConstantExpr init, RefType enclosing, boolean constant where v.getInitializer() = init and init.getEnclosingCallable().getDeclaringType() = enclosing and enclosing.hasQualifiedName("constants", "Values") and - constant = init.(CompileTimeConstantExpr).getBooleanValue() + constant = init.getBooleanValue() select init, constant diff --git a/java/ql/test/library-tests/constants/getIntValue.ql b/java/ql/test/library-tests/constants/getIntValue.ql index dbb2c5bd9670..5fc5b108032f 100644 --- a/java/ql/test/library-tests/constants/getIntValue.ql +++ b/java/ql/test/library-tests/constants/getIntValue.ql @@ -1,9 +1,9 @@ import semmle.code.java.Variable -from Variable v, Expr init, RefType enclosing, int constant +from Variable v, CompileTimeConstantExpr init, RefType enclosing, int constant where v.getInitializer() = init and init.getEnclosingCallable().getDeclaringType() = enclosing and enclosing.hasQualifiedName("constants", "Values") and - constant = init.(CompileTimeConstantExpr).getIntValue() + constant = init.getIntValue() select init, constant diff --git a/java/ql/test/library-tests/constants/getStringValue.expected b/java/ql/test/library-tests/constants/getStringValue.expected index 249fb1c16252..b6afeb8052eb 100644 --- a/java/ql/test/library-tests/constants/getStringValue.expected +++ b/java/ql/test/library-tests/constants/getStringValue.expected @@ -1,3 +1,3 @@ | constants/Values.java:19:29:19:31 | '*' | * | | constants/Values.java:91:37:91:45 | "a" + "b" | ab | -| constants/Values.java:92:29:92:38 | ... + ... | abc | +| constants/Values.java:92:35:92:44 | ... + ... | abc |