From e0444c531b9925bb14f9aae15c4b185f472d2034 Mon Sep 17 00:00:00 2001 From: idrissrio Date: Mon, 29 Sep 2025 13:25:30 +0200 Subject: [PATCH 1/4] Java: Add integration test for constant expr detection --- .../ConstantExpAppearsNonConstant.expected | 1 + .../ConstantExpAppearsNonConstant.qlref | 1 + .../java/evaluation-to-constant-errortype/Test.java | 7 +++++++ .../java/evaluation-to-constant-errortype/test.py | 2 ++ 4 files changed, 11 insertions(+) create mode 100644 java/ql/integration-tests/java/evaluation-to-constant-errortype/ConstantExpAppearsNonConstant.expected create mode 100644 java/ql/integration-tests/java/evaluation-to-constant-errortype/ConstantExpAppearsNonConstant.qlref create mode 100644 java/ql/integration-tests/java/evaluation-to-constant-errortype/Test.java create mode 100644 java/ql/integration-tests/java/evaluation-to-constant-errortype/test.py diff --git a/java/ql/integration-tests/java/evaluation-to-constant-errortype/ConstantExpAppearsNonConstant.expected b/java/ql/integration-tests/java/evaluation-to-constant-errortype/ConstantExpAppearsNonConstant.expected new file mode 100644 index 000000000000..1dcec10c6ee9 --- /dev/null +++ b/java/ql/integration-tests/java/evaluation-to-constant-errortype/ConstantExpAppearsNonConstant.expected @@ -0,0 +1 @@ +| Test.java:3:8:3:15 | | Expression always evaluates to the same value. | diff --git a/java/ql/integration-tests/java/evaluation-to-constant-errortype/ConstantExpAppearsNonConstant.qlref b/java/ql/integration-tests/java/evaluation-to-constant-errortype/ConstantExpAppearsNonConstant.qlref new file mode 100644 index 000000000000..6d7e1f5cb7ff --- /dev/null +++ b/java/ql/integration-tests/java/evaluation-to-constant-errortype/ConstantExpAppearsNonConstant.qlref @@ -0,0 +1 @@ +Likely Bugs/Arithmetic/ConstantExpAppearsNonConstant.ql \ No newline at end of file diff --git a/java/ql/integration-tests/java/evaluation-to-constant-errortype/Test.java b/java/ql/integration-tests/java/evaluation-to-constant-errortype/Test.java new file mode 100644 index 000000000000..913c7817c7f7 --- /dev/null +++ b/java/ql/integration-tests/java/evaluation-to-constant-errortype/Test.java @@ -0,0 +1,7 @@ +class Test { + public static void updateFlashlights(Minecraft mc){ + if(mc.world != null){ + + } + } +} \ No newline at end of file diff --git a/java/ql/integration-tests/java/evaluation-to-constant-errortype/test.py b/java/ql/integration-tests/java/evaluation-to-constant-errortype/test.py new file mode 100644 index 000000000000..759e4cf8b82b --- /dev/null +++ b/java/ql/integration-tests/java/evaluation-to-constant-errortype/test.py @@ -0,0 +1,2 @@ +def test(codeql, java): + codeql.database.create(build_mode="none") \ No newline at end of file From 659afb5f3049c934e03e6b5c1dc93af8e7bb2896 Mon Sep 17 00:00:00 2001 From: idrissrio Date: Mon, 29 Sep 2025 13:28:55 +0200 Subject: [PATCH 2/4] Java: Fix false positives in evaluation-to-constant query for ErrorType --- .../src/Likely Bugs/Arithmetic/ConstantExpAppearsNonConstant.ql | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/java/ql/src/Likely Bugs/Arithmetic/ConstantExpAppearsNonConstant.ql b/java/ql/src/Likely Bugs/Arithmetic/ConstantExpAppearsNonConstant.ql index 50f508626317..094c8bbc70c8 100644 --- a/java/ql/src/Likely Bugs/Arithmetic/ConstantExpAppearsNonConstant.ql +++ b/java/ql/src/Likely Bugs/Arithmetic/ConstantExpAppearsNonConstant.ql @@ -19,7 +19,7 @@ predicate isConstantExp(Expr e) { // A literal is constant. e instanceof Literal or - e instanceof TypeAccess + exists(TypeAccess ta | ta = e | not ta.getType() instanceof ErrorType) or e instanceof ArrayTypeAccess or From b82d8c22521ad141948077cabbde0a3e824edadb Mon Sep 17 00:00:00 2001 From: idrissrio Date: Mon, 29 Sep 2025 13:29:34 +0200 Subject: [PATCH 3/4] Java: Accept new test results after query change --- .../ConstantExpAppearsNonConstant.expected | 1 - 1 file changed, 1 deletion(-) diff --git a/java/ql/integration-tests/java/evaluation-to-constant-errortype/ConstantExpAppearsNonConstant.expected b/java/ql/integration-tests/java/evaluation-to-constant-errortype/ConstantExpAppearsNonConstant.expected index 1dcec10c6ee9..e69de29bb2d1 100644 --- a/java/ql/integration-tests/java/evaluation-to-constant-errortype/ConstantExpAppearsNonConstant.expected +++ b/java/ql/integration-tests/java/evaluation-to-constant-errortype/ConstantExpAppearsNonConstant.expected @@ -1 +0,0 @@ -| Test.java:3:8:3:15 | | Expression always evaluates to the same value. | From 63771110a5dd2dd619ab97e37742ca4837b482ed Mon Sep 17 00:00:00 2001 From: idrissrio Date: Tue, 30 Sep 2025 11:46:37 +0200 Subject: [PATCH 4/4] Java: Address review comment --- .../ConstantExpAppearsNonConstant.qlref | 3 ++- .../Likely Bugs/Arithmetic/ConstantExpAppearsNonConstant.ql | 2 +- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/java/ql/integration-tests/java/evaluation-to-constant-errortype/ConstantExpAppearsNonConstant.qlref b/java/ql/integration-tests/java/evaluation-to-constant-errortype/ConstantExpAppearsNonConstant.qlref index 6d7e1f5cb7ff..6d2b25768e5d 100644 --- a/java/ql/integration-tests/java/evaluation-to-constant-errortype/ConstantExpAppearsNonConstant.qlref +++ b/java/ql/integration-tests/java/evaluation-to-constant-errortype/ConstantExpAppearsNonConstant.qlref @@ -1 +1,2 @@ -Likely Bugs/Arithmetic/ConstantExpAppearsNonConstant.ql \ No newline at end of file +query: Likely Bugs/Arithmetic/ConstantExpAppearsNonConstant.ql +postprocess: utils/test/InlineExpectationsTestQuery.ql \ No newline at end of file diff --git a/java/ql/src/Likely Bugs/Arithmetic/ConstantExpAppearsNonConstant.ql b/java/ql/src/Likely Bugs/Arithmetic/ConstantExpAppearsNonConstant.ql index 094c8bbc70c8..28ae63bfd363 100644 --- a/java/ql/src/Likely Bugs/Arithmetic/ConstantExpAppearsNonConstant.ql +++ b/java/ql/src/Likely Bugs/Arithmetic/ConstantExpAppearsNonConstant.ql @@ -19,7 +19,7 @@ predicate isConstantExp(Expr e) { // A literal is constant. e instanceof Literal or - exists(TypeAccess ta | ta = e | not ta.getType() instanceof ErrorType) + e instanceof TypeAccess and not e.(TypeAccess).getType() instanceof ErrorType or e instanceof ArrayTypeAccess or