flatbuffers icon indicating copy to clipboard operation
flatbuffers copied to clipboard

Exception when reading FlexBuffer fixed untyped Vector [Java, 2.0.3]

Open dennishendriksen opened this issue 2 years ago • 3 comments

package org.molgenis.vcfflatbuffers;

import static com.google.flatbuffers.FlexBuffersBuilder.BUILDER_FLAG_SHARE_ALL;

import com.google.flatbuffers.ByteBufferReadWriteBuf;
import com.google.flatbuffers.FlexBuffers;
import com.google.flatbuffers.FlexBuffers.Vector;
import com.google.flatbuffers.FlexBuffersBuilder;
import java.nio.ByteBuffer;

public class FlexBufferApp {
  public static void main(String[] args) {
    FlexBuffersBuilder builder = new FlexBuffersBuilder(BUILDER_FLAG_SHARE_ALL);

    int vectorRef = builder.startVector();
    builder.putInt(123);

    int nestedVectorRef = builder.startVector();
    builder.putInt(234);
    builder.endVector(null, nestedVectorRef, true, true);

    builder.endVector(null, vectorRef, false, true);
    ByteBuffer byteBuffer = builder.finish();

    Vector vector = FlexBuffers.getRoot(new ByteBufferReadWriteBuf(byteBuffer)).asVector();
    System.out.println(vector.get(0));
  }
}

results in

Exception in thread "main" java.lang.IndexOutOfBoundsException
	at java.base/java.nio.Buffer.checkIndex(Buffer.java:743)
	at java.base/java.nio.HeapByteBuffer.get(HeapByteBuffer.java:169)
	at com.google.flatbuffers.ByteBufferReadWriteBuf.get(ByteBufferReadWriteBuf.java:27)
	at com.google.flatbuffers.FlexBuffers$Vector.get(FlexBuffers.java:1084)
	at org.molgenis.vcfflatbuffers.FlexBufferApp.main(FlexBufferApp.java:27)

changing builder.endVector(null, vectorRef, false, true); to builder.endVector(null, vectorRef, false, false); resolves the issue. I'm not sure whether the issue is an invalid FlexBuffer or a bug in the reading code?

Looking into the code I noticed that the vector type is FBT_KEY, is this correct?

dennishendriksen avatar Jun 20 '22 06:06 dennishendriksen

any thoughts about this @aardappel?

dennishendriksen avatar Jun 28 '22 08:06 dennishendriksen

you should look at

https://github.com/google/flatbuffers/blob/1b90300150c79f2d64e494430156388cacd81b6f/java/com/google/flatbuffers/FlexBuffersBuilder.java#L505

endVector(..., false, true) is saying you want an untyped, but fixed length vector, which is not supported by Flexbuffers.

If you turned on assertions you should see that one go off before the IndexOutOfBoundsException

CasperN avatar Jun 28 '22 15:06 CasperN

@paulovap who wrote this code I think.

Also, if an assert can be triggered by user-facing API and its not always on in Java, it should maybe be an exception so it is seen by the user.

aardappel avatar Jun 28 '22 15:06 aardappel