Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions java/ql/src/Likely Bugs/Comparison/EqualsArray.qhelp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
<p>
The <code>equals</code> and <code>hashCode</code> methods on arrays only consider object identity, not
array contents, which is unlikely to be what is intended.
The same applies to <code>Objects.equals</code> when both arguments are arrays.
</p>

</overview>
Expand Down
14 changes: 10 additions & 4 deletions java/ql/src/Likely Bugs/Comparison/EqualsArray.ql
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,19 @@ import java

from MethodCall ma, Array recvtype, Method m
where
recvtype = ma.getQualifier().getType() and
m = ma.getMethod() and
(
m instanceof HashCodeMethod
recvtype = ma.getQualifier().getType() and
(
m instanceof HashCodeMethod
or
m instanceof EqualsMethod and
haveIntersection(recvtype, ma.getArgument(0).getType().(Array))
)
or
m instanceof EqualsMethod and
haveIntersection(recvtype, ma.getArgument(0).getType().(Array))
m.hasQualifiedName("java.util", "Objects", "equals") and
recvtype = ma.getArgument(0).getType() and
haveIntersection(recvtype, ma.getArgument(1).getType().(Array))
Comment on lines +28 to +30
)
select ma,
"The " + m.getName() +
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
---
category: minorAnalysis
---
* 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.
5 changes: 5 additions & 0 deletions java/ql/test/query-tests/EqualsArray/EqualsArray.expected
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
| ObjectsEquals.java:7:3:7:31 | equals(...) | The equals method on arrays only considers object identity and ignores array contents. |
| ObjectsEquals.java:8:3:8:34 | equals(...) | The equals method on arrays only considers object identity and ignores array contents. |
| ObjectsEquals.java:9:3:9:34 | equals(...) | The equals method on arrays only considers object identity and ignores array contents. |
| ObjectsEquals.java:10:3:10:37 | equals(...) | The equals method on arrays only considers object identity and ignores array contents. |
| ObjectsEquals.java:11:3:11:41 | equals(...) | The equals method on arrays only considers object identity and ignores array contents. |
| Test.java:6:10:6:37 | equals(...) | The equals method on arrays only considers object identity and ignores array contents. |
| Test.java:20:3:20:20 | hashCode(...) | The hashCode method on arrays only considers object identity and ignores array contents. |
29 changes: 29 additions & 0 deletions java/ql/test/query-tests/EqualsArray/ObjectsEquals.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import java.util.Arrays;
import java.util.Objects;

class ObjectsEquals {
void compare(int[] first, int[] second, String[] strings, Object[] objects,
int[][] nested, int[][] otherNested, long[] longs) {
Objects.equals(first, second); // $ Alert
Objects.equals(strings, objects); // $ Alert
Objects.equals(objects, strings); // $ Alert
Objects.equals(nested, otherNested); // $ Alert
java.util.Objects.equals(first, second); // $ Alert

java.util.Objects.equals(first, null);
java.util.Objects.equals(null, first);
java.util.Objects.equals(first, longs);
java.util.Objects.equals(first, "not an array");
java.util.Objects.equals("first", "second");
java.util.Objects.deepEquals(first, second);
Arrays.equals(first, second);
Arrays.deepEquals(nested, otherNested);
CustomObjects.equals(first, second);
}

static class CustomObjects {
static boolean equals(Object first, Object second) {
return java.util.Objects.deepEquals(first, second);
}
}
}