|
26 | 26 | /// FlatBuffer parsing, buffer-offset arithmetic, and encoding-spec lookup. |
27 | 27 | public final class FlatSegmentDecoder { |
28 | 28 |
|
| 29 | + /// Hard cap on array-node recursion depth. The encoded array tree nests through child nodes |
| 30 | + /// (validity, patches, run-ends, dictionary codes/values, …); a crafted or self-referential |
| 31 | + /// FlatBuffer can drive [#convertArrayNode] into unbounded recursion and a [StackOverflowError] |
| 32 | + /// — an `Error`, so it would bypass the [io.github.dfa1.vortex.core.error.VortexException] |
| 33 | + /// contract. 64 is well past any real encoding's nesting. |
| 34 | + static final int MAX_ARRAY_TREE_DEPTH = 64; |
| 35 | + |
29 | 36 | private final ReadRegistry registry; |
30 | 37 |
|
31 | 38 | /// Creates a decoder backed by the given registry. |
@@ -64,20 +71,25 @@ public Array decode(MemorySegment seg, List<String> encodingSpecs, |
64 | 71 | dataOffset += bufDesc.length(); |
65 | 72 | } |
66 | 73 |
|
67 | | - ArrayNode rootNode = convertArrayNode(fbArray.root(), encodingSpecs); |
| 74 | + ArrayNode rootNode = convertArrayNode(fbArray.root(), encodingSpecs, 0); |
68 | 75 | var ctx = new DecodeContext(rootNode, dtype, rowCount, bufs, registry, arena); |
69 | 76 | return registry.decode(ctx); |
70 | 77 | } |
71 | 78 |
|
72 | 79 | private static ArrayNode convertArrayNode( |
73 | 80 | io.github.dfa1.vortex.core.fbs.FbsArrayNode fbs, |
74 | | - List<String> encodingSpecs |
| 81 | + List<String> encodingSpecs, |
| 82 | + int depth |
75 | 83 | ) { |
| 84 | + if (depth > MAX_ARRAY_TREE_DEPTH) { |
| 85 | + throw new io.github.dfa1.vortex.core.error.VortexException( |
| 86 | + "array tree depth exceeds limit (" + MAX_ARRAY_TREE_DEPTH + ")"); |
| 87 | + } |
76 | 88 | String rawEncodingId = encodingSpecs.get(fbs.encoding()); |
77 | 89 |
|
78 | 90 | ArrayNode[] children = new ArrayNode[fbs.childrenLength()]; |
79 | 91 | for (int i = 0; i < children.length; i++) { |
80 | | - children[i] = convertArrayNode(fbs.children(i), encodingSpecs); |
| 92 | + children[i] = convertArrayNode(fbs.children(i), encodingSpecs, depth + 1); |
81 | 93 | } |
82 | 94 |
|
83 | 95 | int[] bufferIndices = new int[fbs.buffersLength()]; |
|
0 commit comments