-
Bug
-
Resolution: Fixed
-
P3
-
repo-panama
The following test is failing:
@NativeStruct("[" +
"[2i32](arr)" +
"i32(x)" +
"](MyStruct)")
public interface MyStruct extends Struct<MyStruct> {
@NativeGetter("arr")
Array<Integer> arr$get();
@NativeSetter("arr")
void arr$set(Array<Integer> val);
@NativeGetter("x")
int x$get();
@NativeSetter("x")
void x$set(int val);
}
@Test
public void testNoOverwrite() {
try(Scope scope = Scope.globalScope().fork()) {
MyStruct str = scope.allocateStruct(MyStruct.class);
str.x$set(10);
Array<Integer> ints = scope.allocateArray(NativeTypes.INT32, new int[] { 1, 2, 3 }); // 1 too big
str.arr$set(ints); // will this overwrite x?
assertEquals(str.x$get(), 10);
}
}
This is because the pointer passed to References.OfArray::set is not limited to the size of the struct's array, and the size of the argument array is used as length for the copy:
static void set(Pointer<?> pointer, Array<?> arrayValue) {
try {
Pointer.copy(arrayValue.elementPointer(), pointer,
arrayValue.bytesSize());
} catch (Throwable ex) {
throw new IllegalStateException(ex);
}
}
This could be fixed by limiting to the size of a struct's field pointer to the size of the type of that field.
@NativeStruct("[" +
"[2i32](arr)" +
"i32(x)" +
"](MyStruct)")
public interface MyStruct extends Struct<MyStruct> {
@NativeGetter("arr")
Array<Integer> arr$get();
@NativeSetter("arr")
void arr$set(Array<Integer> val);
@NativeGetter("x")
int x$get();
@NativeSetter("x")
void x$set(int val);
}
@Test
public void testNoOverwrite() {
try(Scope scope = Scope.globalScope().fork()) {
MyStruct str = scope.allocateStruct(MyStruct.class);
str.x$set(10);
Array<Integer> ints = scope.allocateArray(NativeTypes.INT32, new int[] { 1, 2, 3 }); // 1 too big
str.arr$set(ints); // will this overwrite x?
assertEquals(str.x$get(), 10);
}
}
This is because the pointer passed to References.OfArray::set is not limited to the size of the struct's array, and the size of the argument array is used as length for the copy:
static void set(Pointer<?> pointer, Array<?> arrayValue) {
try {
Pointer.copy(arrayValue.elementPointer(), pointer,
arrayValue.bytesSize());
} catch (Throwable ex) {
throw new IllegalStateException(ex);
}
}
This could be fixed by limiting to the size of a struct's field pointer to the size of the type of that field.