Skip to content

Commit

Permalink
#Rust Add buffer verification
Browse files Browse the repository at this point in the history
  • Loading branch information
candysonya committed Jan 6, 2025
1 parent 977da58 commit e7f5d13
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 110 deletions.
36 changes: 27 additions & 9 deletions rust/reflection/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@ use num::traits::float::Float;
use num::traits::int::PrimInt;
use num::traits::FromPrimitive;
use stdint::uintmax_t;
use std::error;
use stdint::uintmax_t;
use thiserror::Error;

#[derive(Error, Debug)]
Expand Down Expand Up @@ -338,9 +340,14 @@ pub unsafe fn get_any_field_string_in_struct(
/// # Safety
///
/// [buf] must contain a valid root table and valid offset to it.
pub unsafe fn set_any_field_integer(buf: &mut [u8], field: &Field, v: i64) -> FlatbufferResult<()> {
pub unsafe fn set_any_field_integer(
buf: &mut [u8],
table_loc: usize,
field: &Field,
v: i64,
) -> FlatbufferResult<()> {
let field_type = field.type_().base_type();
let table = get_any_root(buf);
let table = Table::follow(buf, table_loc);

let Some(field_loc) = get_field_loc(&table, field) else {
return Err(FlatbufferError::SetValueNotSupported());
Expand All @@ -358,9 +365,14 @@ pub unsafe fn set_any_field_integer(buf: &mut [u8], field: &Field, v: i64) -> Fl
/// # Safety
///
/// [buf] must contain a valid root table and valid offset to it.
pub unsafe fn set_any_field_float(buf: &mut [u8], field: &Field, v: f64) -> FlatbufferResult<()> {
pub unsafe fn set_any_field_float(
buf: &mut [u8],
table_loc: usize,
field: &Field,
v: f64,
) -> FlatbufferResult<()> {
let field_type = field.type_().base_type();
let table = get_any_root(buf);
let table = Table::follow(buf, table_loc);

let Some(field_loc) = get_field_loc(&table, field) else {
return Err(FlatbufferError::SetValueNotSupported());
Expand All @@ -378,9 +390,14 @@ pub unsafe fn set_any_field_float(buf: &mut [u8], field: &Field, v: f64) -> Flat
/// # Safety
///
/// [buf] must contain a valid root table and valid offset to it.
pub unsafe fn set_any_field_string(buf: &mut [u8], field: &Field, v: &str) -> FlatbufferResult<()> {
pub unsafe fn set_any_field_string(
buf: &mut [u8],
table_loc: usize,
field: &Field,
v: &str,
) -> FlatbufferResult<()> {
let field_type = field.type_().base_type();
let table = get_any_root(buf);
let table = Table::follow(buf, table_loc);

let Some(field_loc) = get_field_loc(&table, field) else {
return Err(FlatbufferError::SetValueNotSupported());
Expand All @@ -400,11 +417,12 @@ pub unsafe fn set_any_field_string(buf: &mut [u8], field: &Field, v: &str) -> Fl
/// [buf] must contain a valid root table and valid offset to it.
pub unsafe fn set_field<T: EndianScalar>(
buf: &mut [u8],
table_loc: usize,
field: &Field,
v: T,
) -> FlatbufferResult<()> {
let field_type = field.type_().base_type();
let table = get_any_root(buf);
let table = Table::follow(buf, table_loc);

if !is_scalar(field_type) {
return Err(FlatbufferError::SetValueNotSupported());
Expand Down Expand Up @@ -444,6 +462,7 @@ pub unsafe fn set_field<T: EndianScalar>(
/// [buf] must contain a valid root table and valid offset to it and conform to the [schema].
pub unsafe fn set_string(
buf: &mut Vec<u8>,
table_loc: usize,
field: &Field,
v: &str,
schema: &Schema,
Expand All @@ -460,8 +479,7 @@ pub unsafe fn set_string(
));
}

let table = get_any_root(buf);
let table_loc = table.loc();
let table = Table::follow(buf, table_loc);

let Some(field_loc) = get_field_loc(&table, field) else {
return Err(FlatbufferError::SetValueNotSupported());
Expand Down
Loading

0 comments on commit e7f5d13

Please sign in to comment.