@@ -72,9 +72,8 @@ public final class Variant {
7272 static final int BINARY_SEARCH_THRESHOLD = 32 ;
7373
7474 /**
75- * Create a Variant from a tuple of (value, metadata) byte arrays..
76- * Includes validation of version and a validation of the toplevel
77- * structure.
75+ * Create a Variant from a tuple of (value, metadata) byte arrays.
76+ * Includes validation of the version and the top-level structure.
7877 * @param value value buffer
7978 * @param metadata metadata buffer
8079 * @throws UnsupportedOperationException if there is a version mismatch
@@ -87,8 +86,7 @@ public Variant(byte[] value, byte[] metadata) {
8786 /**
8887 * Create a Variant from a subset of the (value, metadata) buffers
8988 * supplied.
90- * Includes validation of version and a validation of the toplevel
91- * structure.
89+ * Includes validation of the version and the toplevel structure.
9290 * @param value value buffer
9391 * @param valuePos offset where the value data begins
9492 * @param valueLength length of value data
@@ -108,8 +106,7 @@ public Variant(byte[] value, int valuePos, int valueLength, byte[] metadata, int
108106
109107 /**
110108 * Create a Variant from a tuple of (value, metadata) buffers.
111- * Includes validation of version and a validation of the toplevel
112- * structure.
109+ * Includes validation of the version and the toplevel structure.
113110 * @param value value buffer
114111 * @param metadata metadata buffer
115112 * @throws UnsupportedOperationException if there is a version mismatch
@@ -119,6 +116,8 @@ public Variant(ByteBuffer value, ByteBuffer metadata) {
119116 this .value = value .asReadOnlyBuffer ().order (ByteOrder .LITTLE_ENDIAN );
120117 this .metadata = metadata .asReadOnlyBuffer ().order (ByteOrder .LITTLE_ENDIAN );
121118 this .depth = 0 ;
119+ // A metadata buffer must contain at least the version byte
120+ Preconditions .checkArgument (this .metadata .remaining () >= 1 , "variant metadata is empty" );
122121 // There is currently only one allowed version.
123122 if ((metadata .get (metadata .position ()) & VariantUtil .VERSION_MASK ) != VariantUtil .VERSION ) {
124123 throw new UnsupportedOperationException (String .format (
@@ -129,18 +128,14 @@ public Variant(ByteBuffer value, ByteBuffer metadata) {
129128 // Pre-compute dictionary size for lazy metadata cache allocation.
130129 int pos = this .metadata .position ();
131130 int metaOffsetSize = ((this .metadata .get (pos ) >> 6 ) & 0x3 ) + 1 ;
132- if (this .metadata .remaining () > 1 ) {
133- Preconditions .checkArgument (
134- this .metadata .remaining () >= 1 + metaOffsetSize ,
135- "variant metadata truncated: offsetSize=" + metaOffsetSize );
136- this .dictSize = VariantUtil .readUnsignedLittleEndian (this .metadata , pos + 1 , metaOffsetSize );
137- long dictTableEnd = 1L + metaOffsetSize + ((long ) this .dictSize + 1 ) * metaOffsetSize ;
138- Preconditions .checkArgument (
139- dictTableEnd <= this .metadata .remaining (),
140- "variant metadata dictionary extends past buffer: dictSize=" + this .dictSize );
141- } else {
142- this .dictSize = 0 ;
143- }
131+ Preconditions .checkArgument (
132+ this .metadata .remaining () >= 1 + metaOffsetSize ,
133+ "variant metadata truncated: offsetSize=" + metaOffsetSize );
134+ this .dictSize = VariantUtil .readUnsignedLittleEndian (this .metadata , pos + 1 , metaOffsetSize );
135+ long dictTableEnd = 1L + metaOffsetSize + ((long ) this .dictSize + 1 ) * metaOffsetSize ;
136+ Preconditions .checkArgument (
137+ dictTableEnd <= this .metadata .remaining (),
138+ "variant metadata dictionary extends past buffer: dictSize=" + this .dictSize );
144139 this .metadataCache = null ;
145140 VariantUtil .validateValueShallow (this .value , dictSize );
146141 }
@@ -407,6 +402,7 @@ public Variant getElementAtIndex(int index) {
407402
408403 /**
409404 * Creates a child Variant that shares this instance's metadata cache.
405+ * @throws IllegalArgumentException validation error during construction.
410406 */
411407 private Variant childVariant (ByteBuffer childValue ) {
412408 return new Variant (childValue , metadata , metadataCache , dictSize , depth + 1 );
0 commit comments