Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-8218772

Writing to a struct's array field can overwrite trailing fields

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • repo-panama
    • repo-panama
    • tools

      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.

            jvernee Jorn Vernee
            jvernee Jorn Vernee
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: