Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 3 additions & 7 deletions rust/flatbuffers/src/verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -275,7 +275,7 @@ fn trace_elem<T>(res: Result<T>, index: usize, position: usize) -> Result<T> {
}

#[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.
Expand All @@ -289,20 +289,16 @@ 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,
max_tables: 1_000_000,
// size_ might do something different.
max_apparent_size: 1 << 31,
ignore_missing_null_terminator: false,
root_table_name: None,
}
}
}
Expand All @@ -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,
Expand Down
1 change: 1 addition & 0 deletions rust/reflection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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::{
Expand Down
7 changes: 4 additions & 3 deletions rust/reflection/src/reflection_verifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,13 @@ pub fn verify_with_options(
schema: &Schema,
opts: &VerifierOptions,
buf_loc_to_obj_idx: &mut HashMap<usize, i32>,
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 {
Expand Down
6 changes: 4 additions & 2 deletions rust/reflection/src/safe_buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl<'a> SafeBuffer<'a> {
opts: &VerifierOptions,
) -> FlatbufferResult<Self> {
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,
Expand Down Expand Up @@ -141,7 +141,9 @@ impl<'a> SafeTable<'a> {
pub fn get_field_string(&self, field_name: &str) -> FlatbufferResult<Option<&str>> {
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)
}
Expand Down