8323552: AbstractMemorySegmentImpl#mismatch returns -1 when comparing distinct areas of the same instance of MemorySegment

Reviewed-by: mcimadamore
This commit is contained in:
Per Minborg 2024-03-25 09:37:24 +00:00
parent b235682a18
commit 93579c29e3
2 changed files with 30 additions and 7 deletions

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -681,10 +681,6 @@ public abstract sealed class AbstractMemorySegmentImpl
long dstBytes = dstToOffset - dstFromOffset;
srcImpl.checkAccess(srcFromOffset, srcBytes, true);
dstImpl.checkAccess(dstFromOffset, dstBytes, true);
if (dstImpl == srcImpl) {
srcImpl.checkValidState();
return -1;
}
long bytes = Math.min(srcBytes, dstBytes);
long i = 0;

View File

@ -1,5 +1,5 @@
/*
* Copyright (c) 2020, 2023, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2020, 2024, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
@ -23,6 +23,7 @@
/*
* @test
* @bug 8323552
* @run testng TestMismatch
*/
@ -44,7 +45,7 @@ import static org.testng.Assert.assertThrows;
public class TestMismatch {
// stores a increasing sequence of values into the memory of the given segment
// stores an increasing sequence of values into the memory of the given segment
static MemorySegment initializeSegment(MemorySegment segment) {
for (int i = 0 ; i < segment.byteSize() ; i++) {
segment.set(ValueLayout.JAVA_BYTE, i, (byte)i);
@ -278,6 +279,32 @@ public class TestMismatch {
}
}
@Test
public void testSameSegment() {
var segment = MemorySegment.ofArray(new byte[]{
1,2,3,4, 1,2,3,4, 1,4});
long match = MemorySegment.mismatch(
segment, 0L, 4L,
segment, 4L, 8L);
assertEquals(match, -1);
long noMatch = MemorySegment.mismatch(
segment, 0L, 4L,
segment, 1L, 5L);
assertEquals(noMatch, 0);
long noMatchEnd = MemorySegment.mismatch(
segment, 0L, 2L,
segment, 8L, 10L);
assertEquals(noMatchEnd, 1);
long same = MemorySegment.mismatch(
segment, 0L, 8L,
segment, 0L, 8L);
assertEquals(same, -1);
}
enum SegmentKind {
NATIVE(i -> Arena.ofAuto().allocate(i, 1)),
ARRAY(i -> MemorySegment.ofArray(new byte[i]));