Skip to content

Commit aae1644

Browse files
committed
Improve Object-getClass-test-for-primitive.ql
Tested query with Variant Analysis and had one result.
1 parent fdd498c commit aae1644

File tree

2 files changed

+28
-26
lines changed

2 files changed

+28
-26
lines changed

codeql-custom-queries-java/not-tested-queries/Object-getClass-test-for-primitive.ql

Lines changed: 0 additions & 26 deletions
This file was deleted.
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/**
2+
* Finds `Object.getClass()` calls which compare the result with
3+
* a primitive type literal, e.g.:
4+
* ```java
5+
* obj.getClass() == int.class
6+
* ```
7+
*
8+
* Primitives do not exist as `Object` (only their boxed representation
9+
* does), therefore these checks will always fail.
10+
*
11+
* @kind problem
12+
*/
13+
14+
import java
15+
16+
class PrimitiveOrVoidType extends Type {
17+
PrimitiveOrVoidType() {
18+
this instanceof PrimitiveType
19+
or this instanceof VoidType
20+
}
21+
}
22+
23+
from EqualityTest eqTest, MethodAccess getClassCall
24+
where
25+
getClassCall.getMethod().hasStringSignature("getClass()")
26+
and eqTest.getAnOperand() = getClassCall
27+
and eqTest.getAnOperand().(TypeLiteral).getReferencedType() instanceof PrimitiveOrVoidType
28+
select eqTest, "`getClass()` will never return primitive class"

0 commit comments

Comments
 (0)