From d6feb3f126ffff6e4ba76502e1eea02d183b543c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mak=20Naze=C4=8Di=C4=87-Andrlon?= Date: Thu, 10 Apr 2025 12:09:47 +1000 Subject: [PATCH] [Rust] Allow reflection verifier to start with custom root --- rust/flatbuffers/src/verifier.rs | 10 +++------- rust/reflection/src/lib.rs | 1 + rust/reflection/src/reflection_verifier.rs | 7 ++++--- rust/reflection/src/safe_buffer.rs | 6 ++++-- 4 files changed, 12 insertions(+), 12 deletions(-) diff --git a/rust/flatbuffers/src/verifier.rs b/rust/flatbuffers/src/verifier.rs index 4847e8b240..31425f1106 100644 --- a/rust/flatbuffers/src/verifier.rs +++ b/rust/flatbuffers/src/verifier.rs @@ -275,7 +275,7 @@ fn trace_elem(res: Result, index: usize, position: usize) -> Result { } #[derive(Debug, Clone, PartialEq, Eq)] -pub struct VerifierOptions<'a> { +pub struct VerifierOptions { /// Maximum depth of nested tables allowed in a valid flatbuffer. pub max_depth: usize, /// Maximum number of tables allowed in a valid flatbuffer. @@ -289,12 +289,9 @@ pub struct VerifierOptions<'a> { // probably want an option to ignore utf8 errors since strings come from c++ // options to error un-recognized enums and unions? possible footgun. // Ignore nested flatbuffers, etc? - - /// The name of the table to use as the root table instead of the schema root. - pub root_table_name: Option<&'a str>, } -impl Default for VerifierOptions<'_> { +impl Default for VerifierOptions { fn default() -> Self { Self { max_depth: 64, @@ -302,7 +299,6 @@ impl Default for VerifierOptions<'_> { // size_ might do something different. max_apparent_size: 1 << 31, ignore_missing_null_terminator: false, - root_table_name: None, } } } @@ -311,7 +307,7 @@ impl Default for VerifierOptions<'_> { #[derive(Debug)] pub struct Verifier<'opts, 'buf> { buffer: &'buf [u8], - opts: &'opts VerifierOptions<'opts>, + opts: &'opts VerifierOptions, depth: usize, num_tables: usize, apparent_size: usize, diff --git a/rust/reflection/src/lib.rs b/rust/reflection/src/lib.rs index fa345ab3a0..e1fbd860cd 100644 --- a/rust/reflection/src/lib.rs +++ b/rust/reflection/src/lib.rs @@ -22,6 +22,7 @@ pub use vector_of_any::VectorOfAny; mod r#struct; pub use crate::r#struct::Struct; pub use crate::reflection_generated::reflection; +pub use crate::reflection_verifier::verify_with_options; pub use crate::safe_buffer::SafeBuffer; use flatbuffers::{ diff --git a/rust/reflection/src/reflection_verifier.rs b/rust/reflection/src/reflection_verifier.rs index 0f11155e0a..575ba5713e 100644 --- a/rust/reflection/src/reflection_verifier.rs +++ b/rust/reflection/src/reflection_verifier.rs @@ -28,12 +28,13 @@ pub fn verify_with_options( schema: &Schema, opts: &VerifierOptions, buf_loc_to_obj_idx: &mut HashMap, + root_table_name: Option<&str>, ) -> FlatbufferResult<()> { let mut verifier = Verifier::new(opts, buffer); - let root_table = match opts.root_table_name { - Some(root_table_name) => schema + let root_table = match root_table_name { + Some(name) => schema .objects() - .lookup_by_key(root_table_name, |o, k| o.key_compare_with_value(k)), + .lookup_by_key(name, |o, k| o.key_compare_with_value(k)), None => schema.root_table(), }; if let Some(table_object) = root_table { diff --git a/rust/reflection/src/safe_buffer.rs b/rust/reflection/src/safe_buffer.rs index a892d5ddce..66abe0c43f 100644 --- a/rust/reflection/src/safe_buffer.rs +++ b/rust/reflection/src/safe_buffer.rs @@ -49,7 +49,7 @@ impl<'a> SafeBuffer<'a> { opts: &VerifierOptions, ) -> FlatbufferResult { let mut buf_loc_to_obj_idx = HashMap::new(); - verify_with_options(&buf, schema, opts, &mut buf_loc_to_obj_idx)?; + verify_with_options(&buf, schema, opts, &mut buf_loc_to_obj_idx, None)?; Ok(SafeBuffer { buf, schema, @@ -141,7 +141,9 @@ impl<'a> SafeTable<'a> { pub fn get_field_string(&self, field_name: &str) -> FlatbufferResult> { if let Some(field) = self.safe_buf.find_field_by_name(self.loc, field_name)? { // SAFETY: the buffer was verified during construction. - Ok(Some(unsafe { get_field_string(&Table::new(&self.safe_buf.buf, self.loc), &field) })) + Ok(Some(unsafe { + get_field_string(&Table::new(&self.safe_buf.buf, self.loc), &field) + })) } else { Err(FlatbufferError::FieldNotFound) }