Skip to content

Commit 47aca46

Browse files
committed
Allow verification of buffers starting from specified root
1 parent c1cb450 commit 47aca46

File tree

2 files changed

+15
-5
lines changed

2 files changed

+15
-5
lines changed

rust/flatbuffers/src/verifier.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ fn trace_elem<T>(res: Result<T>, index: usize, position: usize) -> Result<T> {
275275
}
276276

277277
#[derive(Debug, Clone, PartialEq, Eq)]
278-
pub struct VerifierOptions {
278+
pub struct VerifierOptions<'a> {
279279
/// Maximum depth of nested tables allowed in a valid flatbuffer.
280280
pub max_depth: usize,
281281
/// Maximum number of tables allowed in a valid flatbuffer.
@@ -289,16 +289,20 @@ pub struct VerifierOptions {
289289
// probably want an option to ignore utf8 errors since strings come from c++
290290
// options to error un-recognized enums and unions? possible footgun.
291291
// Ignore nested flatbuffers, etc?
292+
293+
/// The name of the table to use as the root table instead of the schema root.
294+
pub root_table_name: Option<&'a str>,
292295
}
293296

294-
impl Default for VerifierOptions {
297+
impl Default for VerifierOptions<'_> {
295298
fn default() -> Self {
296299
Self {
297300
max_depth: 64,
298301
max_tables: 1_000_000,
299302
// size_ might do something different.
300303
max_apparent_size: 1 << 31,
301304
ignore_missing_null_terminator: false,
305+
root_table_name: None,
302306
}
303307
}
304308
}
@@ -307,7 +311,7 @@ impl Default for VerifierOptions {
307311
#[derive(Debug)]
308312
pub struct Verifier<'opts, 'buf> {
309313
buffer: &'buf [u8],
310-
opts: &'opts VerifierOptions,
314+
opts: &'opts VerifierOptions<'opts>,
311315
depth: usize,
312316
num_tables: usize,
313317
apparent_size: usize,

rust/reflection/src/reflection_verifier.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,14 @@ pub fn verify_with_options(
3030
buf_loc_to_obj_idx: &mut HashMap<usize, i32>,
3131
) -> FlatbufferResult<()> {
3232
let mut verifier = Verifier::new(opts, buffer);
33-
if let Some(table_object) = schema.root_table() {
34-
if let core::result::Result::Ok(table_pos) = verifier.get_uoffset(0) {
33+
let root_table = match opts.root_table_name {
34+
Some(root_table_name) => schema
35+
.objects()
36+
.lookup_by_key(root_table_name, |o, k| o.key_compare_with_value(k)),
37+
None => schema.root_table(),
38+
};
39+
if let Some(table_object) = root_table {
40+
if let Ok(table_pos) = verifier.get_uoffset(0) {
3541
// Inserts -1 as object index for root table
3642
buf_loc_to_obj_idx.insert(table_pos.try_into()?, -1);
3743
let mut verified = vec![false; buffer.len()];

0 commit comments

Comments
 (0)