Skip to content

Commit 26b3d7d

Browse files
authored
Merge pull request #24385 from harship04/fix-arraycopy-nullrestricted-npe
Fix NullPointerException handling for null-restricted arrays in arraycopy
2 parents 63779da + 38bd0a1 commit 26b3d7d

2 files changed

Lines changed: 12 additions & 7 deletions

File tree

runtime/vm/ValueTypeHelpers.hpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -594,6 +594,11 @@ class VM_ValueTypeHelpers {
594594
srcObject = VM_VMHelpers::popObjectInSpecialFrame(currentThread);
595595
}
596596

597+
/* Null cannot be stored into a null-restricted array. */
598+
if ((NULL == copyObject) && J9_IS_J9ARRAYCLASS_NULL_RESTRICTED((J9ArrayClass *)destClazz)) {
599+
return -2;
600+
}
601+
597602
if (typeChecksRequired) {
598603
if (!VM_VMHelpers::objectArrayStoreAllowed(currentThread, destObject, copyObject)) {
599604
return -1;

test/functional/Valhalla/src_qtypes/org/openj9/test/lworld/ValueTypeSystemArraycopyTests.java

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -649,21 +649,21 @@ static public void testSystemArrayCopy22() throws Throwable {
649649
try {
650650
initArraysForASETest(); // ifArray3[ARRAY_SIZE/2] is NULL
651651
testIFVT(ifArray3, nullRestrictedVtArrayDst);
652-
} catch (java.lang.ArrayStoreException ase1) {
652+
} catch (java.lang.ArrayStoreException | java.lang.NullPointerException ex1) {
653653
try {
654654
checkResultsPartial(ifArray3, nullRestrictedVtArrayDst, ARRAY_SIZE/2);
655655
checkNullRestrictedVTArrayAfterException(ARRAY_SIZE/2);
656656

657657
initArraysForASETest();
658658
testIFVT(ifArray3, nullRestrictedVtArrayDst);
659-
} catch (java.lang.ArrayStoreException ase2) {
659+
} catch (java.lang.ArrayStoreException | java.lang.NullPointerException ex2) {
660660
checkResultsPartial(ifArray3, nullRestrictedVtArrayDst, ARRAY_SIZE/2);
661661
// pass
662662
return;
663663
}
664664
}
665665

666-
Assert.fail("Expect an ArrayStoreException. No exception or wrong kind of exception thrown");
666+
Assert.fail("Expect an ArrayStoreException or NullPointerException. No exception or wrong kind of exception thrown");
667667
}
668668

669669
@Test(priority=1)
@@ -672,22 +672,22 @@ static public void testSystemArrayCopy23() throws Throwable {
672672
try {
673673
initArraysForASETest(); // ifArray3[ARRAY_SIZE/2] is NULL
674674
testIFIF(ifArray3, nullRestrictedVtArrayDst);
675-
} catch (java.lang.ArrayStoreException ase1) {
675+
} catch (java.lang.ArrayStoreException | java.lang.NullPointerException ex1) {
676676
try {
677677
checkResultsPartial(ifArray3, nullRestrictedVtArrayDst, ARRAY_SIZE/2);
678678
checkNullRestrictedVTArrayAfterException(ARRAY_SIZE/2);
679679

680680
initArraysForASETest();
681681
testIFIF(ifArray3, nullRestrictedVtArrayDst);
682-
} catch (java.lang.ArrayStoreException ase2) {
682+
} catch (java.lang.ArrayStoreException | java.lang.NullPointerException ex2) {
683683
checkResultsPartial(ifArray3, nullRestrictedVtArrayDst, ARRAY_SIZE/2);
684684
checkNullRestrictedVTArrayAfterException(ARRAY_SIZE/2);
685685
// pass
686686
return;
687687
}
688688
}
689689

690-
Assert.fail("Expect a ArrayStoreException. No exception or wrong kind of exception thrown");
690+
Assert.fail("Expect an ArrayStoreException or NullPointerException. No exception or wrong kind of exception thrown");
691691
}
692692

693693
@Test(priority=1)
@@ -776,7 +776,7 @@ static public void testSystemArrayCopy28() throws Throwable {
776776
checkResults(nullRestrictedVtArraySrc, vtArrayDst);
777777
}
778778

779-
@Test(priority=1, invocationCount=2, expectedExceptions=ArrayStoreException.class)
779+
@Test(priority=1, invocationCount=2, expectedExceptions={ArrayStoreException.class, NullPointerException.class})
780780
static public void testSystemArrayCopy29() throws Throwable {
781781
initArraysToCopyNullToNullRestrictedArray();
782782
testVTVT(vtArraySrc, nullRestrictedVtArrayDst);

0 commit comments

Comments
 (0)