Skip to content

Commit d6feb3f

Browse files
committed
[Rust] Allow reflection verifier to start with custom root
1 parent 817bab9 commit d6feb3f

File tree

4 files changed

+12
-12
lines changed

4 files changed

+12
-12
lines changed

rust/flatbuffers/src/verifier.rs

+3-7
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<'a> {
278+
pub struct VerifierOptions {
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,20 +289,16 @@ pub struct VerifierOptions<'a> {
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>,
295292
}
296293

297-
impl Default for VerifierOptions<'_> {
294+
impl Default for VerifierOptions {
298295
fn default() -> Self {
299296
Self {
300297
max_depth: 64,
301298
max_tables: 1_000_000,
302299
// size_ might do something different.
303300
max_apparent_size: 1 << 31,
304301
ignore_missing_null_terminator: false,
305-
root_table_name: None,
306302
}
307303
}
308304
}
@@ -311,7 +307,7 @@ impl Default for VerifierOptions<'_> {
311307
#[derive(Debug)]
312308
pub struct Verifier<'opts, 'buf> {
313309
buffer: &'buf [u8],
314-
opts: &'opts VerifierOptions<'opts>,
310+
opts: &'opts VerifierOptions,
315311
depth: usize,
316312
num_tables: usize,
317313
apparent_size: usize,

rust/reflection/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ pub use vector_of_any::VectorOfAny;
2222
mod r#struct;
2323
pub use crate::r#struct::Struct;
2424
pub use crate::reflection_generated::reflection;
25+
pub use crate::reflection_verifier::verify_with_options;
2526
pub use crate::safe_buffer::SafeBuffer;
2627

2728
use flatbuffers::{

rust/reflection/src/reflection_verifier.rs

+4-3
Original file line numberDiff line numberDiff line change
@@ -28,12 +28,13 @@ pub fn verify_with_options(
2828
schema: &Schema,
2929
opts: &VerifierOptions,
3030
buf_loc_to_obj_idx: &mut HashMap<usize, i32>,
31+
root_table_name: Option<&str>,
3132
) -> FlatbufferResult<()> {
3233
let mut verifier = Verifier::new(opts, buffer);
33-
let root_table = match opts.root_table_name {
34-
Some(root_table_name) => schema
34+
let root_table = match root_table_name {
35+
Some(name) => schema
3536
.objects()
36-
.lookup_by_key(root_table_name, |o, k| o.key_compare_with_value(k)),
37+
.lookup_by_key(name, |o, k| o.key_compare_with_value(k)),
3738
None => schema.root_table(),
3839
};
3940
if let Some(table_object) = root_table {

rust/reflection/src/safe_buffer.rs

+4-2
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl<'a> SafeBuffer<'a> {
4949
opts: &VerifierOptions,
5050
) -> FlatbufferResult<Self> {
5151
let mut buf_loc_to_obj_idx = HashMap::new();
52-
verify_with_options(&buf, schema, opts, &mut buf_loc_to_obj_idx)?;
52+
verify_with_options(&buf, schema, opts, &mut buf_loc_to_obj_idx, None)?;
5353
Ok(SafeBuffer {
5454
buf,
5555
schema,
@@ -141,7 +141,9 @@ impl<'a> SafeTable<'a> {
141141
pub fn get_field_string(&self, field_name: &str) -> FlatbufferResult<Option<&str>> {
142142
if let Some(field) = self.safe_buf.find_field_by_name(self.loc, field_name)? {
143143
// SAFETY: the buffer was verified during construction.
144-
Ok(Some(unsafe { get_field_string(&Table::new(&self.safe_buf.buf, self.loc), &field) }))
144+
Ok(Some(unsafe {
145+
get_field_string(&Table::new(&self.safe_buf.buf, self.loc), &field)
146+
}))
145147
} else {
146148
Err(FlatbufferError::FieldNotFound)
147149
}

0 commit comments

Comments
 (0)