This is an automated email from the ASF dual-hosted git repository.
chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-fury.git
The following commit(s) were added to refs/heads/main by this push:
new e9678c53 fix(java): Prevent exception in
ObjectArray.clearObjectArray() (#1573)
e9678c53 is described below
commit e9678c5352365114733bef7bd1df1aa2645a8948
Author: Tommy Ettinger <[email protected]>
AuthorDate: Wed Apr 24 22:17:40 2024 -0700
fix(java): Prevent exception in ObjectArray.clearObjectArray() (#1573)
## What does this PR do?
Without this change, if `ObjectArray.clearObjectArray()` is called with
start > 0 and size < 128 (which is `COPY_THRESHOLD`), then an
IllegalArgumentException is thrown. This happens because `Arrays.fill()`
takes a from index and a to index, in contrast with
`clearObjectArray()`, which takes a start index and a size. The
exception looks like this:
```
Exception in thread "main" java.lang.IllegalArgumentException:
fromIndex(11) > toIndex(5)
at java.base/java.util.Arrays.rangeCheck(Arrays.java:718)
at java.base/java.util.Arrays.fill(Arrays.java:3453)
```
This wasn't thrown in Fury code, because Fury itself never calls
clearObjectArray() with any start value other than 0. The method is part
of a public API, though, so user code can call it.
I added an extra check to ObjectArrayTest that calls clearObjectArray()
with 1 as a start value, instead of only testing with a start value of
0. I can run the tests one at a time and the only test I touched passes,
but (maybe because of the commit I checked out) running the command in
CONTRIBUTING.md (`mvn -T10 clean test`) fails on
`org.apache.fury.serializer.MetaSharedCompatibleTest`. I'm not sure if
there's anything I can do about that; the issue may already be fixed.
Spotless and Checkstyle have both been run and found no issues.
## Related issues
None.
## Does this PR introduce any user-facing change?
No.
- [ ] Does this PR introduce any public API change?
- [ ] Does this PR introduce any binary protocol compatibility change?
## Benchmark
I don't see any way this could have a significant effect on performance.
It only adds one addition performed once during some calls to an O(n)
function, and clearing an array can't be done multiple times in sequence
with any difference from doing it once.
---
.../src/main/java/org/apache/fury/collection/ObjectArray.java | 2 +-
.../test/java/org/apache/fury/collection/ObjectArrayTest.java | 10 ++++++++++
2 files changed, 11 insertions(+), 1 deletion(-)
diff --git
a/java/fury-core/src/main/java/org/apache/fury/collection/ObjectArray.java
b/java/fury-core/src/main/java/org/apache/fury/collection/ObjectArray.java
index 578f9c56..ff70d740 100644
--- a/java/fury-core/src/main/java/org/apache/fury/collection/ObjectArray.java
+++ b/java/fury-core/src/main/java/org/apache/fury/collection/ObjectArray.java
@@ -95,7 +95,7 @@ public final class ObjectArray {
*/
public static void clearObjectArray(Object[] objects, int start, int size) {
if (size < COPY_THRESHOLD) {
- Arrays.fill(objects, start, size, null);
+ Arrays.fill(objects, start, start + size, null);
} else {
if (size < NIL_ARRAY_SIZE) {
System.arraycopy(NIL_ARRAY, 0, objects, start, size);
diff --git
a/java/fury-core/src/test/java/org/apache/fury/collection/ObjectArrayTest.java
b/java/fury-core/src/test/java/org/apache/fury/collection/ObjectArrayTest.java
index 3c2dc12c..ebac186a 100644
---
a/java/fury-core/src/test/java/org/apache/fury/collection/ObjectArrayTest.java
+++
b/java/fury-core/src/test/java/org/apache/fury/collection/ObjectArrayTest.java
@@ -55,6 +55,16 @@ public class ObjectArrayTest {
throw new IllegalStateException(String.format("numObj: %d, index:
%d", numObj, i));
}
}
+ for (int i = 0; i < numObj; i++) {
+ array[i] = o;
+ }
+ ObjectArray.clearObjectArray(array, 1, array.length - 1);
+ for (int i = 1; i < array.length; i++) {
+ Object value = array[i];
+ if (value != null) {
+ throw new IllegalStateException(String.format("numObj: %d, index:
%d", numObj, i));
+ }
+ }
}
}
}
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]