mirror of
https://github.com/google/flatbuffers.git
synced 2026-06-15 16:57:29 +00:00
Rust soundness fixes (#7518)
* Rust soundness fixes * Second pass * Make init_from_table unsafe * Remove SafeSliceAccess * Clippy * Remove create_vector_of_strings * More clippy * Remove deprecated root type accessors * More soundness fixes * Fix EndianScalar for bool * Add TriviallyTransmutable * Add debug assertions * Review comments * Review feedback
This commit is contained in:
committed by
GitHub
parent
dadbff5714
commit
374f8fb5fb
@@ -59,10 +59,8 @@ impl core::fmt::Debug for OptionalByte {
|
||||
impl<'a> flatbuffers::Follow<'a> for OptionalByte {
|
||||
type Inner = Self;
|
||||
#[inline]
|
||||
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
let b = unsafe {
|
||||
flatbuffers::read_scalar_at::<i8>(buf, loc)
|
||||
};
|
||||
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
let b = flatbuffers::read_scalar_at::<i8>(buf, loc);
|
||||
Self(b)
|
||||
}
|
||||
}
|
||||
@@ -70,21 +68,21 @@ impl<'a> flatbuffers::Follow<'a> for OptionalByte {
|
||||
impl flatbuffers::Push for OptionalByte {
|
||||
type Output = OptionalByte;
|
||||
#[inline]
|
||||
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
|
||||
unsafe { flatbuffers::emplace_scalar::<i8>(dst, self.0); }
|
||||
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
|
||||
flatbuffers::emplace_scalar::<i8>(dst, self.0);
|
||||
}
|
||||
}
|
||||
|
||||
impl flatbuffers::EndianScalar for OptionalByte {
|
||||
type Scalar = i8;
|
||||
#[inline]
|
||||
fn to_little_endian(self) -> Self {
|
||||
let b = i8::to_le(self.0);
|
||||
Self(b)
|
||||
fn to_little_endian(self) -> i8 {
|
||||
self.0.to_le()
|
||||
}
|
||||
#[inline]
|
||||
#[allow(clippy::wrong_self_convention)]
|
||||
fn from_little_endian(self) -> Self {
|
||||
let b = i8::from_le(self.0);
|
||||
fn from_little_endian(v: i8) -> Self {
|
||||
let b = i8::from_le(v);
|
||||
Self(b)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -19,8 +19,8 @@ pub struct ScalarStuff<'a> {
|
||||
impl<'a> flatbuffers::Follow<'a> for ScalarStuff<'a> {
|
||||
type Inner = ScalarStuff<'a>;
|
||||
#[inline]
|
||||
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
Self { _tab: flatbuffers::Table { buf, loc } }
|
||||
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
|
||||
Self { _tab: flatbuffers::Table::new(buf, loc) }
|
||||
}
|
||||
}
|
||||
|
||||
@@ -67,7 +67,7 @@ impl<'a> ScalarStuff<'a> {
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
|
||||
pub unsafe fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
|
||||
ScalarStuff { _tab: table }
|
||||
}
|
||||
#[allow(unused_mut)]
|
||||
@@ -194,147 +194,255 @@ impl<'a> ScalarStuff<'a> {
|
||||
|
||||
#[inline]
|
||||
pub fn just_i8(&self) -> i8 {
|
||||
self._tab.get::<i8>(ScalarStuff::VT_JUST_I8, Some(0)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i8>(ScalarStuff::VT_JUST_I8, Some(0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn maybe_i8(&self) -> Option<i8> {
|
||||
self._tab.get::<i8>(ScalarStuff::VT_MAYBE_I8, None)
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i8>(ScalarStuff::VT_MAYBE_I8, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn default_i8(&self) -> i8 {
|
||||
self._tab.get::<i8>(ScalarStuff::VT_DEFAULT_I8, Some(42)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i8>(ScalarStuff::VT_DEFAULT_I8, Some(42)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn just_u8(&self) -> u8 {
|
||||
self._tab.get::<u8>(ScalarStuff::VT_JUST_U8, Some(0)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<u8>(ScalarStuff::VT_JUST_U8, Some(0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn maybe_u8(&self) -> Option<u8> {
|
||||
self._tab.get::<u8>(ScalarStuff::VT_MAYBE_U8, None)
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<u8>(ScalarStuff::VT_MAYBE_U8, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn default_u8(&self) -> u8 {
|
||||
self._tab.get::<u8>(ScalarStuff::VT_DEFAULT_U8, Some(42)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<u8>(ScalarStuff::VT_DEFAULT_U8, Some(42)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn just_i16(&self) -> i16 {
|
||||
self._tab.get::<i16>(ScalarStuff::VT_JUST_I16, Some(0)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i16>(ScalarStuff::VT_JUST_I16, Some(0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn maybe_i16(&self) -> Option<i16> {
|
||||
self._tab.get::<i16>(ScalarStuff::VT_MAYBE_I16, None)
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i16>(ScalarStuff::VT_MAYBE_I16, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn default_i16(&self) -> i16 {
|
||||
self._tab.get::<i16>(ScalarStuff::VT_DEFAULT_I16, Some(42)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i16>(ScalarStuff::VT_DEFAULT_I16, Some(42)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn just_u16(&self) -> u16 {
|
||||
self._tab.get::<u16>(ScalarStuff::VT_JUST_U16, Some(0)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<u16>(ScalarStuff::VT_JUST_U16, Some(0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn maybe_u16(&self) -> Option<u16> {
|
||||
self._tab.get::<u16>(ScalarStuff::VT_MAYBE_U16, None)
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<u16>(ScalarStuff::VT_MAYBE_U16, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn default_u16(&self) -> u16 {
|
||||
self._tab.get::<u16>(ScalarStuff::VT_DEFAULT_U16, Some(42)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<u16>(ScalarStuff::VT_DEFAULT_U16, Some(42)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn just_i32(&self) -> i32 {
|
||||
self._tab.get::<i32>(ScalarStuff::VT_JUST_I32, Some(0)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i32>(ScalarStuff::VT_JUST_I32, Some(0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn maybe_i32(&self) -> Option<i32> {
|
||||
self._tab.get::<i32>(ScalarStuff::VT_MAYBE_I32, None)
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i32>(ScalarStuff::VT_MAYBE_I32, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn default_i32(&self) -> i32 {
|
||||
self._tab.get::<i32>(ScalarStuff::VT_DEFAULT_I32, Some(42)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i32>(ScalarStuff::VT_DEFAULT_I32, Some(42)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn just_u32(&self) -> u32 {
|
||||
self._tab.get::<u32>(ScalarStuff::VT_JUST_U32, Some(0)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<u32>(ScalarStuff::VT_JUST_U32, Some(0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn maybe_u32(&self) -> Option<u32> {
|
||||
self._tab.get::<u32>(ScalarStuff::VT_MAYBE_U32, None)
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<u32>(ScalarStuff::VT_MAYBE_U32, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn default_u32(&self) -> u32 {
|
||||
self._tab.get::<u32>(ScalarStuff::VT_DEFAULT_U32, Some(42)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<u32>(ScalarStuff::VT_DEFAULT_U32, Some(42)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn just_i64(&self) -> i64 {
|
||||
self._tab.get::<i64>(ScalarStuff::VT_JUST_I64, Some(0)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i64>(ScalarStuff::VT_JUST_I64, Some(0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn maybe_i64(&self) -> Option<i64> {
|
||||
self._tab.get::<i64>(ScalarStuff::VT_MAYBE_I64, None)
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i64>(ScalarStuff::VT_MAYBE_I64, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn default_i64(&self) -> i64 {
|
||||
self._tab.get::<i64>(ScalarStuff::VT_DEFAULT_I64, Some(42)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<i64>(ScalarStuff::VT_DEFAULT_I64, Some(42)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn just_u64(&self) -> u64 {
|
||||
self._tab.get::<u64>(ScalarStuff::VT_JUST_U64, Some(0)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<u64>(ScalarStuff::VT_JUST_U64, Some(0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn maybe_u64(&self) -> Option<u64> {
|
||||
self._tab.get::<u64>(ScalarStuff::VT_MAYBE_U64, None)
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<u64>(ScalarStuff::VT_MAYBE_U64, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn default_u64(&self) -> u64 {
|
||||
self._tab.get::<u64>(ScalarStuff::VT_DEFAULT_U64, Some(42)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<u64>(ScalarStuff::VT_DEFAULT_U64, Some(42)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn just_f32(&self) -> f32 {
|
||||
self._tab.get::<f32>(ScalarStuff::VT_JUST_F32, Some(0.0)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<f32>(ScalarStuff::VT_JUST_F32, Some(0.0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn maybe_f32(&self) -> Option<f32> {
|
||||
self._tab.get::<f32>(ScalarStuff::VT_MAYBE_F32, None)
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<f32>(ScalarStuff::VT_MAYBE_F32, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn default_f32(&self) -> f32 {
|
||||
self._tab.get::<f32>(ScalarStuff::VT_DEFAULT_F32, Some(42.0)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<f32>(ScalarStuff::VT_DEFAULT_F32, Some(42.0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn just_f64(&self) -> f64 {
|
||||
self._tab.get::<f64>(ScalarStuff::VT_JUST_F64, Some(0.0)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<f64>(ScalarStuff::VT_JUST_F64, Some(0.0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn maybe_f64(&self) -> Option<f64> {
|
||||
self._tab.get::<f64>(ScalarStuff::VT_MAYBE_F64, None)
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<f64>(ScalarStuff::VT_MAYBE_F64, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn default_f64(&self) -> f64 {
|
||||
self._tab.get::<f64>(ScalarStuff::VT_DEFAULT_F64, Some(42.0)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<f64>(ScalarStuff::VT_DEFAULT_F64, Some(42.0)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn just_bool(&self) -> bool {
|
||||
self._tab.get::<bool>(ScalarStuff::VT_JUST_BOOL, Some(false)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<bool>(ScalarStuff::VT_JUST_BOOL, Some(false)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn maybe_bool(&self) -> Option<bool> {
|
||||
self._tab.get::<bool>(ScalarStuff::VT_MAYBE_BOOL, None)
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<bool>(ScalarStuff::VT_MAYBE_BOOL, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn default_bool(&self) -> bool {
|
||||
self._tab.get::<bool>(ScalarStuff::VT_DEFAULT_BOOL, Some(true)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<bool>(ScalarStuff::VT_DEFAULT_BOOL, Some(true)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn just_enum(&self) -> OptionalByte {
|
||||
self._tab.get::<OptionalByte>(ScalarStuff::VT_JUST_ENUM, Some(OptionalByte::None)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<OptionalByte>(ScalarStuff::VT_JUST_ENUM, Some(OptionalByte::None)).unwrap()}
|
||||
}
|
||||
#[inline]
|
||||
pub fn maybe_enum(&self) -> Option<OptionalByte> {
|
||||
self._tab.get::<OptionalByte>(ScalarStuff::VT_MAYBE_ENUM, None)
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<OptionalByte>(ScalarStuff::VT_MAYBE_ENUM, None)}
|
||||
}
|
||||
#[inline]
|
||||
pub fn default_enum(&self) -> OptionalByte {
|
||||
self._tab.get::<OptionalByte>(ScalarStuff::VT_DEFAULT_ENUM, Some(OptionalByte::One)).unwrap()
|
||||
// Safety:
|
||||
// Created from valid Table for this object
|
||||
// which contains a valid value in this slot
|
||||
unsafe { self._tab.get::<OptionalByte>(ScalarStuff::VT_DEFAULT_ENUM, Some(OptionalByte::One)).unwrap()}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -836,18 +944,6 @@ impl ScalarStuffT {
|
||||
})
|
||||
}
|
||||
}
|
||||
#[inline]
|
||||
#[deprecated(since="2.0.0", note="Deprecated in favor of `root_as...` methods.")]
|
||||
pub fn get_root_as_scalar_stuff<'a>(buf: &'a [u8]) -> ScalarStuff<'a> {
|
||||
unsafe { flatbuffers::root_unchecked::<ScalarStuff<'a>>(buf) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
#[deprecated(since="2.0.0", note="Deprecated in favor of `root_as...` methods.")]
|
||||
pub fn get_size_prefixed_root_as_scalar_stuff<'a>(buf: &'a [u8]) -> ScalarStuff<'a> {
|
||||
unsafe { flatbuffers::size_prefixed_root_unchecked::<ScalarStuff<'a>>(buf) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
/// Verifies that a buffer of bytes contains a `ScalarStuff`
|
||||
/// and returns it.
|
||||
|
||||
Reference in New Issue
Block a user