Skip to content

Commit

Permalink
Catch and bail with the original array when given an inconsistent com…
Browse files Browse the repository at this point in the history
…parator.
  • Loading branch information
DanielRosenwasser authored Jun 25, 2024
1 parent 0823b10 commit 2e898a4
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions rhino/src/main/java/org/mozilla/javascript/NativeArray.java
Original file line number Diff line number Diff line change
Expand Up @@ -1349,8 +1349,16 @@ public int compare(final Object x, final Object y) {
working[i] = getRawElem(o, i);
}

// 'Arrays.sort' is guaranteed to be stable.
Arrays.sort(working, comparator);
// Java's 'Arrays.sort' is guaranteed to be stable so we can use it; however,
// if the comparator is not consistent, it throws an IllegalArgumentException.
// In case where the comparator is not consistent, the ECMAScript specification states
// that sort order is implementation-defined, so we can just return the original array.
try {
Arrays.sort(working, comparator);
}
catch (IllegalArgumentException e) {
return o;
}

// copy the working array back into thisObj
for (int i = 0; i < length; ++i) {
Expand Down

0 comments on commit 2e898a4

Please sign in to comment.