mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-04 12:43:24 +00:00
bulk code format fix (#8707)
This commit is contained in:
@@ -84,12 +84,7 @@ pub unsafe fn get_field_integer<T: for<'a> Follow<'a, Inner = T> + PrimInt + Fro
|
||||
if size_of::<T>() != get_type_size(field.type_().base_type()) {
|
||||
return Err(FlatbufferError::FieldTypeMismatch(
|
||||
std::any::type_name::<T>().to_string(),
|
||||
field
|
||||
.type_()
|
||||
.base_type()
|
||||
.variant_name()
|
||||
.unwrap_or_default()
|
||||
.to_string(),
|
||||
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -109,12 +104,7 @@ pub unsafe fn get_field_float<T: for<'a> Follow<'a, Inner = T> + Float>(
|
||||
if size_of::<T>() != get_type_size(field.type_().base_type()) {
|
||||
return Err(FlatbufferError::FieldTypeMismatch(
|
||||
std::any::type_name::<T>().to_string(),
|
||||
field
|
||||
.type_()
|
||||
.base_type()
|
||||
.variant_name()
|
||||
.unwrap_or_default()
|
||||
.to_string(),
|
||||
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -134,12 +124,7 @@ pub unsafe fn get_field_string<'a>(
|
||||
if field.type_().base_type() != BaseType::String {
|
||||
return Err(FlatbufferError::FieldTypeMismatch(
|
||||
String::from("String"),
|
||||
field
|
||||
.type_()
|
||||
.base_type()
|
||||
.variant_name()
|
||||
.unwrap_or_default()
|
||||
.to_string(),
|
||||
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -160,12 +145,7 @@ pub unsafe fn get_field_struct<'a>(
|
||||
if field.type_().base_type() != BaseType::Obj {
|
||||
return Err(FlatbufferError::FieldTypeMismatch(
|
||||
String::from("Obj"),
|
||||
field
|
||||
.type_()
|
||||
.base_type()
|
||||
.variant_name()
|
||||
.unwrap_or_default()
|
||||
.to_string(),
|
||||
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -186,12 +166,7 @@ pub unsafe fn get_field_vector<'a, T: Follow<'a, Inner = T>>(
|
||||
{
|
||||
return Err(FlatbufferError::FieldTypeMismatch(
|
||||
std::any::type_name::<T>().to_string(),
|
||||
field
|
||||
.type_()
|
||||
.base_type()
|
||||
.variant_name()
|
||||
.unwrap_or_default()
|
||||
.to_string(),
|
||||
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -210,12 +185,7 @@ pub unsafe fn get_field_table<'a>(
|
||||
if field.type_().base_type() != BaseType::Obj {
|
||||
return Err(FlatbufferError::FieldTypeMismatch(
|
||||
String::from("Obj"),
|
||||
field
|
||||
.type_()
|
||||
.base_type()
|
||||
.variant_name()
|
||||
.unwrap_or_default()
|
||||
.to_string(),
|
||||
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -282,12 +252,7 @@ pub unsafe fn get_field_struct_in_struct<'a>(
|
||||
if field.type_().base_type() != BaseType::Obj {
|
||||
return Err(FlatbufferError::FieldTypeMismatch(
|
||||
String::from("Obj"),
|
||||
field
|
||||
.type_()
|
||||
.base_type()
|
||||
.variant_name()
|
||||
.unwrap_or_default()
|
||||
.to_string(),
|
||||
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
@@ -442,15 +407,13 @@ pub unsafe fn set_field<T: EndianScalar>(
|
||||
};
|
||||
|
||||
if buf.len() < field_loc.saturating_add(get_type_size(field_type)) {
|
||||
return Err(FlatbufferError::VerificationError(
|
||||
InvalidFlatbuffer::RangeOutOfBounds {
|
||||
range: core::ops::Range {
|
||||
start: field_loc,
|
||||
end: field_loc.saturating_add(get_type_size(field_type)),
|
||||
},
|
||||
error_trace: Default::default(),
|
||||
return Err(FlatbufferError::VerificationError(InvalidFlatbuffer::RangeOutOfBounds {
|
||||
range: core::ops::Range {
|
||||
start: field_loc,
|
||||
end: field_loc.saturating_add(get_type_size(field_type)),
|
||||
},
|
||||
));
|
||||
error_trace: Default::default(),
|
||||
}));
|
||||
}
|
||||
|
||||
// SAFETY: the buffer range was verified above.
|
||||
@@ -488,49 +451,37 @@ pub unsafe fn set_string(
|
||||
};
|
||||
|
||||
if buf.len() < field_loc + get_type_size(field_type) {
|
||||
return Err(FlatbufferError::VerificationError(
|
||||
InvalidFlatbuffer::RangeOutOfBounds {
|
||||
range: core::ops::Range {
|
||||
start: field_loc,
|
||||
end: field_loc.saturating_add(get_type_size(field_type)),
|
||||
},
|
||||
error_trace: Default::default(),
|
||||
return Err(FlatbufferError::VerificationError(InvalidFlatbuffer::RangeOutOfBounds {
|
||||
range: core::ops::Range {
|
||||
start: field_loc,
|
||||
end: field_loc.saturating_add(get_type_size(field_type)),
|
||||
},
|
||||
));
|
||||
error_trace: Default::default(),
|
||||
}));
|
||||
}
|
||||
|
||||
// SAFETY: the buffer range was verified above.
|
||||
let string_loc = unsafe { deref_uoffset(buf, field_loc)? };
|
||||
if buf.len() < string_loc.saturating_add(SIZE_UOFFSET) {
|
||||
return Err(FlatbufferError::VerificationError(
|
||||
InvalidFlatbuffer::RangeOutOfBounds {
|
||||
range: core::ops::Range {
|
||||
start: string_loc,
|
||||
end: string_loc.saturating_add(SIZE_UOFFSET),
|
||||
},
|
||||
error_trace: Default::default(),
|
||||
return Err(FlatbufferError::VerificationError(InvalidFlatbuffer::RangeOutOfBounds {
|
||||
range: core::ops::Range {
|
||||
start: string_loc,
|
||||
end: string_loc.saturating_add(SIZE_UOFFSET),
|
||||
},
|
||||
));
|
||||
error_trace: Default::default(),
|
||||
}));
|
||||
}
|
||||
|
||||
// SAFETY: the buffer range was verified above.
|
||||
let len_old = unsafe { read_uoffset(buf, string_loc) };
|
||||
if buf.len()
|
||||
< string_loc
|
||||
.saturating_add(SIZE_UOFFSET)
|
||||
.saturating_add(len_old.try_into()?)
|
||||
{
|
||||
return Err(FlatbufferError::VerificationError(
|
||||
InvalidFlatbuffer::RangeOutOfBounds {
|
||||
range: core::ops::Range {
|
||||
start: string_loc,
|
||||
end: string_loc
|
||||
.saturating_add(SIZE_UOFFSET)
|
||||
.saturating_add(len_old.try_into()?),
|
||||
},
|
||||
error_trace: Default::default(),
|
||||
if buf.len() < string_loc.saturating_add(SIZE_UOFFSET).saturating_add(len_old.try_into()?) {
|
||||
return Err(FlatbufferError::VerificationError(InvalidFlatbuffer::RangeOutOfBounds {
|
||||
range: core::ops::Range {
|
||||
start: string_loc,
|
||||
end: string_loc.saturating_add(SIZE_UOFFSET).saturating_add(len_old.try_into()?),
|
||||
},
|
||||
));
|
||||
error_trace: Default::default(),
|
||||
}));
|
||||
}
|
||||
|
||||
let len_new = v.len();
|
||||
@@ -628,9 +579,7 @@ unsafe fn get_any_value_integer(
|
||||
BaseType::ULong => i64::from_u64(u64::follow(buf, loc)),
|
||||
BaseType::Float => i64::from_f32(f32::follow(buf, loc)),
|
||||
BaseType::Double => i64::from_f64(f64::follow(buf, loc)),
|
||||
BaseType::String => ForwardsUOffset::<&str>::follow(buf, loc)
|
||||
.parse::<i64>()
|
||||
.ok(),
|
||||
BaseType::String => ForwardsUOffset::<&str>::follow(buf, loc).parse::<i64>().ok(),
|
||||
_ => None, // Tables & vectors do not make sense.
|
||||
}
|
||||
.ok_or(FlatbufferError::FieldTypeMismatch(
|
||||
@@ -661,9 +610,7 @@ unsafe fn get_any_value_float(
|
||||
BaseType::ULong => f64::from_u64(u64::follow(buf, loc)),
|
||||
BaseType::Float => f64::from_f32(f32::follow(buf, loc)),
|
||||
BaseType::Double => Some(f64::follow(buf, loc)),
|
||||
BaseType::String => ForwardsUOffset::<&str>::follow(buf, loc)
|
||||
.parse::<f64>()
|
||||
.ok(),
|
||||
BaseType::String => ForwardsUOffset::<&str>::follow(buf, loc).parse::<f64>().ok(),
|
||||
_ => None,
|
||||
}
|
||||
.ok_or(FlatbufferError::FieldTypeMismatch(
|
||||
@@ -685,9 +632,9 @@ unsafe fn get_any_value_string(
|
||||
type_index: usize,
|
||||
) -> String {
|
||||
match base_type {
|
||||
BaseType::Float | BaseType::Double => get_any_value_float(base_type, buf, loc)
|
||||
.unwrap_or_default()
|
||||
.to_string(),
|
||||
BaseType::Float | BaseType::Double => {
|
||||
get_any_value_float(base_type, buf, loc).unwrap_or_default().to_string()
|
||||
}
|
||||
BaseType::String => {
|
||||
String::from_utf8_lossy(ForwardsUOffset::<&[u8]>::follow(buf, loc)).to_string()
|
||||
}
|
||||
@@ -728,9 +675,7 @@ unsafe fn get_any_value_string(
|
||||
}
|
||||
BaseType::Vector => String::from("[(elements)]"), // TODO inherited from C++: implement this as well.
|
||||
BaseType::Union => String::from("(union)"), // TODO inherited from C++: implement this as well.
|
||||
_ => get_any_value_integer(base_type, buf, loc)
|
||||
.unwrap_or_default()
|
||||
.to_string(),
|
||||
_ => get_any_value_integer(base_type, buf, loc).unwrap_or_default().to_string(),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -742,15 +687,13 @@ fn set_any_value_integer(
|
||||
v: i64,
|
||||
) -> FlatbufferResult<()> {
|
||||
if buf.len() < get_type_size(base_type) {
|
||||
return Err(FlatbufferError::VerificationError(
|
||||
InvalidFlatbuffer::RangeOutOfBounds {
|
||||
range: core::ops::Range {
|
||||
start: field_loc,
|
||||
end: field_loc.saturating_add(get_type_size(base_type)),
|
||||
},
|
||||
error_trace: Default::default(),
|
||||
return Err(FlatbufferError::VerificationError(InvalidFlatbuffer::RangeOutOfBounds {
|
||||
range: core::ops::Range {
|
||||
start: field_loc,
|
||||
end: field_loc.saturating_add(get_type_size(base_type)),
|
||||
},
|
||||
));
|
||||
error_trace: Default::default(),
|
||||
}));
|
||||
}
|
||||
let buf = &mut buf[field_loc..];
|
||||
let type_name = base_type.variant_name().unwrap_or_default().to_string();
|
||||
@@ -761,10 +704,7 @@ fn set_any_value_integer(
|
||||
// SAFETY: buffer size is verified at the beginning of this function.
|
||||
unsafe { Ok(emplace_scalar::<$ty>(buf, v)) }
|
||||
} else {
|
||||
Err(FlatbufferError::FieldTypeMismatch(
|
||||
String::from("i64"),
|
||||
type_name,
|
||||
))
|
||||
Err(FlatbufferError::FieldTypeMismatch(String::from("i64"), type_name))
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -804,10 +744,7 @@ fn set_any_value_integer(
|
||||
// SAFETY: buffer size is verified at the beginning of this function.
|
||||
unsafe { Ok(emplace_scalar::<f32>(buf, value)) }
|
||||
} else {
|
||||
Err(FlatbufferError::FieldTypeMismatch(
|
||||
String::from("i64"),
|
||||
type_name,
|
||||
))
|
||||
Err(FlatbufferError::FieldTypeMismatch(String::from("i64"), type_name))
|
||||
}
|
||||
}
|
||||
BaseType::Double => {
|
||||
@@ -815,10 +752,7 @@ fn set_any_value_integer(
|
||||
// SAFETY: buffer size is verified at the beginning of this function.
|
||||
unsafe { Ok(emplace_scalar::<f64>(buf, value)) }
|
||||
} else {
|
||||
Err(FlatbufferError::FieldTypeMismatch(
|
||||
String::from("i64"),
|
||||
type_name,
|
||||
))
|
||||
Err(FlatbufferError::FieldTypeMismatch(String::from("i64"), type_name))
|
||||
}
|
||||
}
|
||||
_ => Err(FlatbufferError::SetValueNotSupported),
|
||||
@@ -833,15 +767,13 @@ fn set_any_value_float(
|
||||
v: f64,
|
||||
) -> FlatbufferResult<()> {
|
||||
if buf.len() < get_type_size(base_type) {
|
||||
return Err(FlatbufferError::VerificationError(
|
||||
InvalidFlatbuffer::RangeOutOfBounds {
|
||||
range: core::ops::Range {
|
||||
start: field_loc,
|
||||
end: field_loc.saturating_add(get_type_size(base_type)),
|
||||
},
|
||||
error_trace: Default::default(),
|
||||
return Err(FlatbufferError::VerificationError(InvalidFlatbuffer::RangeOutOfBounds {
|
||||
range: core::ops::Range {
|
||||
start: field_loc,
|
||||
end: field_loc.saturating_add(get_type_size(base_type)),
|
||||
},
|
||||
));
|
||||
error_trace: Default::default(),
|
||||
}));
|
||||
}
|
||||
let buf = &mut buf[field_loc..];
|
||||
let type_name = base_type.variant_name().unwrap_or_default().to_string();
|
||||
@@ -936,10 +868,7 @@ fn set_any_value_float(
|
||||
}
|
||||
_ => return Err(FlatbufferError::SetValueNotSupported),
|
||||
}
|
||||
return Err(FlatbufferError::FieldTypeMismatch(
|
||||
String::from("f64"),
|
||||
type_name,
|
||||
));
|
||||
return Err(FlatbufferError::FieldTypeMismatch(String::from("f64"), type_name));
|
||||
}
|
||||
|
||||
fn is_scalar(base_type: BaseType) -> bool {
|
||||
@@ -1002,10 +931,7 @@ unsafe fn update_offset(
|
||||
}
|
||||
|
||||
if field_type == BaseType::Obj
|
||||
&& schema
|
||||
.objects()
|
||||
.get(field.type_().index().try_into()?)
|
||||
.is_struct()
|
||||
&& schema.objects().get(field.type_().index().try_into()?).is_struct()
|
||||
{
|
||||
continue;
|
||||
}
|
||||
@@ -1041,10 +967,7 @@ unsafe fn update_offset(
|
||||
continue;
|
||||
}
|
||||
if elem_type == BaseType::Obj
|
||||
&& schema
|
||||
.objects()
|
||||
.get(field.type_().index().try_into()?)
|
||||
.is_struct()
|
||||
&& schema.objects().get(field.type_().index().try_into()?).is_struct()
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
File diff suppressed because it is too large
Load Diff
@@ -156,12 +156,7 @@ fn verify_table(
|
||||
}
|
||||
_ => {
|
||||
return Err(FlatbufferError::TypeNotSupported(
|
||||
field
|
||||
.type_()
|
||||
.base_type()
|
||||
.variant_name()
|
||||
.unwrap_or_default()
|
||||
.to_string(),
|
||||
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
|
||||
));
|
||||
}
|
||||
};
|
||||
@@ -342,12 +337,7 @@ fn verify_vector<'a, 'b, 'c>(
|
||||
}
|
||||
_ => {
|
||||
return Err(FlatbufferError::TypeNotSupported(
|
||||
field
|
||||
.type_()
|
||||
.base_type()
|
||||
.variant_name()
|
||||
.unwrap_or_default()
|
||||
.to_string(),
|
||||
field.type_().base_type().variant_name().unwrap_or_default().to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
@@ -401,11 +391,7 @@ fn verify_union<'a, 'b, 'c>(
|
||||
}
|
||||
_ => {
|
||||
return Err(FlatbufferError::TypeNotSupported(
|
||||
enum_type
|
||||
.base_type()
|
||||
.variant_name()
|
||||
.unwrap_or_default()
|
||||
.to_string(),
|
||||
enum_type.base_type().variant_name().unwrap_or_default().to_string(),
|
||||
))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -49,11 +49,7 @@ impl<'a> SafeBuffer<'a> {
|
||||
) -> FlatbufferResult<Self> {
|
||||
let mut buf_loc_to_obj_idx = HashMap::new();
|
||||
verify_with_options(&buf, schema, opts, &mut buf_loc_to_obj_idx)?;
|
||||
Ok(SafeBuffer {
|
||||
buf,
|
||||
schema,
|
||||
buf_loc_to_obj_idx,
|
||||
})
|
||||
Ok(SafeBuffer { buf, schema, buf_loc_to_obj_idx })
|
||||
}
|
||||
|
||||
/// Gets the root table in the buffer.
|
||||
@@ -61,10 +57,7 @@ impl<'a> SafeBuffer<'a> {
|
||||
// SAFETY: the buffer was verified during construction.
|
||||
let table = unsafe { get_any_root(self.buf) };
|
||||
|
||||
SafeTable {
|
||||
safe_buf: self,
|
||||
loc: table.loc(),
|
||||
}
|
||||
SafeTable { safe_buf: self, loc: table.loc() }
|
||||
}
|
||||
|
||||
fn find_field_by_name(
|
||||
@@ -74,9 +67,7 @@ impl<'a> SafeBuffer<'a> {
|
||||
) -> FlatbufferResult<Option<Field>> {
|
||||
Ok(self
|
||||
.get_all_fields(buf_loc)?
|
||||
.lookup_by_key(field_name, |field: &Field<'_>, key| {
|
||||
field.key_compare_with_value(key)
|
||||
}))
|
||||
.lookup_by_key(field_name, |field: &Field<'_>, key| field.key_compare_with_value(key)))
|
||||
}
|
||||
|
||||
fn get_all_fields(&self, buf_loc: usize) -> FlatbufferResult<Vector<ForwardsUOffset<Field>>> {
|
||||
@@ -154,10 +145,7 @@ impl<'a> SafeTable<'a> {
|
||||
// SAFETY: the buffer was verified during construction.
|
||||
let optional_st =
|
||||
unsafe { get_field_struct(&Table::new(&self.safe_buf.buf, self.loc), &field)? };
|
||||
Ok(optional_st.map(|st| SafeStruct {
|
||||
safe_buf: self.safe_buf,
|
||||
loc: st.loc(),
|
||||
}))
|
||||
Ok(optional_st.map(|st| SafeStruct { safe_buf: self.safe_buf, loc: st.loc() }))
|
||||
} else {
|
||||
Err(FlatbufferError::FieldNotFound)
|
||||
}
|
||||
@@ -188,10 +176,7 @@ impl<'a> SafeTable<'a> {
|
||||
// SAFETY: the buffer was verified during construction.
|
||||
let optional_table =
|
||||
unsafe { get_field_table(&Table::new(&self.safe_buf.buf, self.loc), &field)? };
|
||||
Ok(optional_table.map(|t| SafeTable {
|
||||
safe_buf: self.safe_buf,
|
||||
loc: t.loc(),
|
||||
}))
|
||||
Ok(optional_table.map(|t| SafeTable { safe_buf: self.safe_buf, loc: t.loc() }))
|
||||
} else {
|
||||
Err(FlatbufferError::FieldNotFound)
|
||||
}
|
||||
@@ -260,10 +245,7 @@ impl<'a> SafeStruct<'a> {
|
||||
let st = unsafe {
|
||||
get_field_struct_in_struct(&Struct::new(&self.safe_buf.buf, self.loc), &field)?
|
||||
};
|
||||
Ok(SafeStruct {
|
||||
safe_buf: self.safe_buf,
|
||||
loc: st.loc(),
|
||||
})
|
||||
Ok(SafeStruct { safe_buf: self.safe_buf, loc: st.loc() })
|
||||
} else {
|
||||
Err(FlatbufferError::FieldNotFound)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user