Skip to content

Commit c9d9a32

Browse files
authored
Merge pull request #23169 from zhangxinyao88/codex/java-objects-equals-arrays
Java: Detect Objects.equals calls on arrays
2 parents a470cf8 + 31f2a47 commit c9d9a32

5 files changed

Lines changed: 49 additions & 4 deletions

File tree

‎java/ql/src/Likely Bugs/Comparison/EqualsArray.qhelp‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
<p>
88
The <code>equals</code> and <code>hashCode</code> methods on arrays only consider object identity, not
99
array contents, which is unlikely to be what is intended.
10+
The same applies to <code>Objects.equals</code> when both arguments are arrays.
1011
</p>
1112

1213
</overview>

‎java/ql/src/Likely Bugs/Comparison/EqualsArray.ql‎

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,13 +15,19 @@ import java
1515

1616
from MethodCall ma, Array recvtype, Method m
1717
where
18-
recvtype = ma.getQualifier().getType() and
1918
m = ma.getMethod() and
2019
(
21-
m instanceof HashCodeMethod
20+
recvtype = ma.getQualifier().getType() and
21+
(
22+
m instanceof HashCodeMethod
23+
or
24+
m instanceof EqualsMethod and
25+
haveIntersection(recvtype, ma.getArgument(0).getType().(Array))
26+
)
2227
or
23-
m instanceof EqualsMethod and
24-
haveIntersection(recvtype, ma.getArgument(0).getType().(Array))
28+
m.hasQualifiedName("java.util", "Objects", "equals") and
29+
recvtype = ma.getArgument(0).getType() and
30+
haveIntersection(recvtype, ma.getArgument(1).getType().(Array))
2531
)
2632
select ma,
2733
"The " + m.getName() +
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
---
2+
category: minorAnalysis
3+
---
4+
* The `java/equals-on-arrays` query now detects calls to `java.util.Objects.equals` with array arguments, which compare object identity instead of array contents.
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,7 @@
1+
| ObjectsEquals.java:7:3:7:31 | equals(...) | The equals method on arrays only considers object identity and ignores array contents. |
2+
| ObjectsEquals.java:8:3:8:34 | equals(...) | The equals method on arrays only considers object identity and ignores array contents. |
3+
| ObjectsEquals.java:9:3:9:34 | equals(...) | The equals method on arrays only considers object identity and ignores array contents. |
4+
| ObjectsEquals.java:10:3:10:37 | equals(...) | The equals method on arrays only considers object identity and ignores array contents. |
5+
| ObjectsEquals.java:11:3:11:41 | equals(...) | The equals method on arrays only considers object identity and ignores array contents. |
16
| Test.java:6:10:6:37 | equals(...) | The equals method on arrays only considers object identity and ignores array contents. |
27
| Test.java:20:3:20:20 | hashCode(...) | The hashCode method on arrays only considers object identity and ignores array contents. |
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
import java.util.Arrays;
2+
import java.util.Objects;
3+
4+
class ObjectsEquals {
5+
void compare(int[] first, int[] second, String[] strings, Object[] objects,
6+
int[][] nested, int[][] otherNested, long[] longs) {
7+
Objects.equals(first, second); // $ Alert
8+
Objects.equals(strings, objects); // $ Alert
9+
Objects.equals(objects, strings); // $ Alert
10+
Objects.equals(nested, otherNested); // $ Alert
11+
java.util.Objects.equals(first, second); // $ Alert
12+
13+
java.util.Objects.equals(first, null);
14+
java.util.Objects.equals(null, first);
15+
java.util.Objects.equals(first, longs);
16+
java.util.Objects.equals(first, "not an array");
17+
java.util.Objects.equals("first", "second");
18+
java.util.Objects.deepEquals(first, second);
19+
Arrays.equals(first, second);
20+
Arrays.deepEquals(nested, otherNested);
21+
CustomObjects.equals(first, second);
22+
}
23+
24+
static class CustomObjects {
25+
static boolean equals(Object first, Object second) {
26+
return java.util.Objects.deepEquals(first, second);
27+
}
28+
}
29+
}

0 commit comments

Comments
 (0)