@@ -61,6 +61,8 @@ public final class Variant {
6161 * Lazy cache for the parsed array header.
6262 */
6363 private VariantUtil .ArrayInfo cachedArrayInfo ;
64+ /** Nesting depth of this Variant relative to the top-level value (0 = top-level). */
65+ private final int depth ;
6466
6567 /**
6668 * The threshold to switch from linear search to binary search when looking up a field by key in
@@ -69,10 +71,31 @@ public final class Variant {
6971 */
7072 static final int BINARY_SEARCH_THRESHOLD = 32 ;
7173
74+ /**
75+ * Create a Variant from a tuple of (value, metadata) byte arrays.
76+ * Includes validation of the version and the top-level structure.
77+ * @param value value buffer
78+ * @param metadata metadata buffer
79+ * @throws UnsupportedOperationException if there is a version mismatch
80+ * @throws IllegalArgumentException for any validation failure.
81+ */
7282 public Variant (byte [] value , byte [] metadata ) {
7383 this (value , 0 , value .length , metadata , 0 , metadata .length );
7484 }
7585
86+ /**
87+ * Create a Variant from a subset of the (value, metadata) buffers
88+ * supplied.
89+ * Includes validation of the version and the toplevel structure.
90+ * @param value value buffer
91+ * @param valuePos offset where the value data begins
92+ * @param valueLength length of value data
93+ * @param metadata metadata buffer
94+ * @param metadataPos offset where the metadata begins.
95+ * @param metadataLength length of the metadata.
96+ * @throws UnsupportedOperationException if there is a version mismatch
97+ * @throws IllegalArgumentException for any validation failure.
98+ */
7699 public Variant (byte [] value , int valuePos , int valueLength , byte [] metadata , int metadataPos , int metadataLength ) {
77100 this (ByteBuffer .wrap (value , valuePos , valueLength ), ByteBuffer .wrap (metadata , metadataPos , metadataLength ));
78101 }
@@ -81,10 +104,20 @@ public Variant(byte[] value, int valuePos, int valueLength, byte[] metadata, int
81104 this (value , metadata .getEncodedBuffer ());
82105 }
83106
107+ /**
108+ * Create a Variant from a tuple of (value, metadata) buffers.
109+ * Includes validation of the version and the toplevel structure.
110+ * @param value value buffer
111+ * @param metadata metadata buffer
112+ * @throws UnsupportedOperationException if there is a version mismatch
113+ * @throws IllegalArgumentException for any validation failure.
114+ */
84115 public Variant (ByteBuffer value , ByteBuffer metadata ) {
85116 this .value = value .asReadOnlyBuffer ().order (ByteOrder .LITTLE_ENDIAN );
86117 this .metadata = metadata .asReadOnlyBuffer ().order (ByteOrder .LITTLE_ENDIAN );
87-
118+ 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" );
88121 // There is currently only one allowed version.
89122 if ((metadata .get (metadata .position ()) & VariantUtil .VERSION_MASK ) != VariantUtil .VERSION ) {
90123 throw new UnsupportedOperationException (String .format (
@@ -95,29 +128,39 @@ public Variant(ByteBuffer value, ByteBuffer metadata) {
95128 // Pre-compute dictionary size for lazy metadata cache allocation.
96129 int pos = this .metadata .position ();
97130 int metaOffsetSize = ((this .metadata .get (pos ) >> 6 ) & 0x3 ) + 1 ;
98- if (this .metadata .remaining () > 1 ) {
99- Preconditions .checkArgument (
100- this .metadata .remaining () >= 1 + metaOffsetSize ,
101- "variant metadata truncated: offsetSize=" + metaOffsetSize );
102- this .dictSize = VariantUtil .readUnsignedLittleEndian (this .metadata , pos + 1 , metaOffsetSize );
103- long dictTableEnd = 1L + metaOffsetSize + ((long ) this .dictSize + 1 ) * metaOffsetSize ;
104- Preconditions .checkArgument (
105- dictTableEnd <= this .metadata .remaining (),
106- "variant metadata dictionary extends past buffer: dictSize=" + this .dictSize );
107- } else {
108- this .dictSize = 0 ;
109- }
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 );
110139 this .metadataCache = null ;
140+ VariantUtil .validateValueShallow (this .value , dictSize );
111141 }
112142
113143 /**
114- * Package-private constructor that shares pre-parsed metadata state from a parent Variant.
144+ * Package-private constructor that shares pre-parsed metadata state from a parent Variant, performing shallow validation of the structure.
145+ * @param value value buffer
146+ * @param metadata metadata buffer
147+ * @param metadataCache shared metadata cache.
148+ * @param dictSize shared dictionary size.
149+ * @param depth depth of this variant in a recursive structure.
150+ * @throws IllegalArgumentException if the depth of variants is too high or the structure
151+ * invalid in some other form.
115152 */
116- Variant (ByteBuffer value , ByteBuffer metadata , String [] metadataCache , int dictSize ) {
153+ Variant (ByteBuffer value , ByteBuffer metadata , String [] metadataCache , int dictSize , int depth ) {
154+ Preconditions .checkArgument (
155+ depth <= VariantUtil .MAX_VARIANT_DEPTH ,
156+ "variant nesting depth exceeds maximum %s" ,
157+ VariantUtil .MAX_VARIANT_DEPTH );
117158 this .value = value .asReadOnlyBuffer ().order (ByteOrder .LITTLE_ENDIAN );
118159 this .metadata = metadata .asReadOnlyBuffer ().order (ByteOrder .LITTLE_ENDIAN );
119160 this .metadataCache = metadataCache ;
120161 this .dictSize = dictSize ;
162+ this .depth = depth ;
163+ VariantUtil .validateValueShallow (this .value , dictSize );
121164 }
122165
123166 public ByteBuffer getValueBuffer () {
@@ -359,9 +402,10 @@ public Variant getElementAtIndex(int index) {
359402
360403 /**
361404 * Creates a child Variant that shares this instance's metadata cache.
405+ * @throws IllegalArgumentException validation error during construction.
362406 */
363407 private Variant childVariant (ByteBuffer childValue ) {
364- return new Variant (childValue , metadata , metadataCache , dictSize );
408+ return new Variant (childValue , metadata , metadataCache , dictSize , depth + 1 );
365409 }
366410
367411 /**
0 commit comments