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:
Raphael Taylor-Davies
2022-09-29 14:58:49 +01:00
committed by GitHub
parent dadbff5714
commit 374f8fb5fb
102 changed files with 2673 additions and 2035 deletions

View File

@@ -59,10 +59,8 @@ impl core::fmt::Debug for ABC {
impl<'a> flatbuffers::Follow<'a> for ABC {
type Inner = Self;
#[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let b = unsafe {
flatbuffers::read_scalar_at::<i32>(buf, loc)
};
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let b = flatbuffers::read_scalar_at::<i32>(buf, loc);
Self(b)
}
}
@@ -70,21 +68,21 @@ impl<'a> flatbuffers::Follow<'a> for ABC {
impl flatbuffers::Push for ABC {
type Output = ABC;
#[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
unsafe { flatbuffers::emplace_scalar::<i32>(dst, self.0); }
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
flatbuffers::emplace_scalar::<i32>(dst, self.0);
}
}
impl flatbuffers::EndianScalar for ABC {
type Scalar = i32;
#[inline]
fn to_little_endian(self) -> Self {
let b = i32::to_le(self.0);
Self(b)
fn to_little_endian(self) -> i32 {
self.0.to_le()
}
#[inline]
#[allow(clippy::wrong_self_convention)]
fn from_little_endian(self) -> Self {
let b = i32::from_le(self.0);
fn from_little_endian(v: i32) -> Self {
let b = i32::from_le(v);
Self(b)
}
}

View File

@@ -19,8 +19,8 @@ pub struct KeywordsInTable<'a> {
impl<'a> flatbuffers::Follow<'a> for KeywordsInTable<'a> {
type Inner = KeywordsInTable<'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) }
}
}
@@ -35,7 +35,7 @@ impl<'a> KeywordsInTable<'a> {
}
#[inline]
pub fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
pub unsafe fn init_from_table(table: flatbuffers::Table<'a>) -> Self {
KeywordsInTable { _tab: table }
}
#[allow(unused_mut)]
@@ -66,19 +66,31 @@ impl<'a> KeywordsInTable<'a> {
#[inline]
pub fn is(&self) -> ABC {
self._tab.get::<ABC>(KeywordsInTable::VT_IS, Some(ABC::void)).unwrap()
// Safety:
// Created from valid Table for this object
// which contains a valid value in this slot
unsafe { self._tab.get::<ABC>(KeywordsInTable::VT_IS, Some(ABC::void)).unwrap()}
}
#[inline]
pub fn private(&self) -> public {
self._tab.get::<public>(KeywordsInTable::VT_PRIVATE, Some(public::NONE)).unwrap()
// Safety:
// Created from valid Table for this object
// which contains a valid value in this slot
unsafe { self._tab.get::<public>(KeywordsInTable::VT_PRIVATE, Some(public::NONE)).unwrap()}
}
#[inline]
pub fn type_(&self) -> i32 {
self._tab.get::<i32>(KeywordsInTable::VT_TYPE_, Some(0)).unwrap()
// Safety:
// Created from valid Table for this object
// which contains a valid value in this slot
unsafe { self._tab.get::<i32>(KeywordsInTable::VT_TYPE_, Some(0)).unwrap()}
}
#[inline]
pub fn default(&self) -> bool {
self._tab.get::<bool>(KeywordsInTable::VT_DEFAULT, Some(false)).unwrap()
// Safety:
// Created from valid Table for this object
// which contains a valid value in this slot
unsafe { self._tab.get::<bool>(KeywordsInTable::VT_DEFAULT, Some(false)).unwrap()}
}
}

View File

@@ -59,10 +59,8 @@ impl core::fmt::Debug for KeywordsInUnion {
impl<'a> flatbuffers::Follow<'a> for KeywordsInUnion {
type Inner = Self;
#[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let b = unsafe {
flatbuffers::read_scalar_at::<u8>(buf, loc)
};
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let b = flatbuffers::read_scalar_at::<u8>(buf, loc);
Self(b)
}
}
@@ -70,21 +68,21 @@ impl<'a> flatbuffers::Follow<'a> for KeywordsInUnion {
impl flatbuffers::Push for KeywordsInUnion {
type Output = KeywordsInUnion;
#[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
unsafe { flatbuffers::emplace_scalar::<u8>(dst, self.0); }
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
flatbuffers::emplace_scalar::<u8>(dst, self.0);
}
}
impl flatbuffers::EndianScalar for KeywordsInUnion {
type Scalar = u8;
#[inline]
fn to_little_endian(self) -> Self {
let b = u8::to_le(self.0);
Self(b)
fn to_little_endian(self) -> u8 {
self.0.to_le()
}
#[inline]
#[allow(clippy::wrong_self_convention)]
fn from_little_endian(self) -> Self {
let b = u8::from_le(self.0);
fn from_little_endian(v: u8) -> Self {
let b = u8::from_le(v);
Self(b)
}
}

View File

@@ -51,10 +51,8 @@ impl core::fmt::Debug for public {
impl<'a> flatbuffers::Follow<'a> for public {
type Inner = Self;
#[inline]
fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let b = unsafe {
flatbuffers::read_scalar_at::<i32>(buf, loc)
};
unsafe fn follow(buf: &'a [u8], loc: usize) -> Self::Inner {
let b = flatbuffers::read_scalar_at::<i32>(buf, loc);
Self(b)
}
}
@@ -62,21 +60,21 @@ impl<'a> flatbuffers::Follow<'a> for public {
impl flatbuffers::Push for public {
type Output = public;
#[inline]
fn push(&self, dst: &mut [u8], _rest: &[u8]) {
unsafe { flatbuffers::emplace_scalar::<i32>(dst, self.0); }
unsafe fn push(&self, dst: &mut [u8], _written_len: usize) {
flatbuffers::emplace_scalar::<i32>(dst, self.0);
}
}
impl flatbuffers::EndianScalar for public {
type Scalar = i32;
#[inline]
fn to_little_endian(self) -> Self {
let b = i32::to_le(self.0);
Self(b)
fn to_little_endian(self) -> i32 {
self.0.to_le()
}
#[inline]
#[allow(clippy::wrong_self_convention)]
fn from_little_endian(self) -> Self {
let b = i32::from_le(self.0);
fn from_little_endian(v: i32) -> Self {
let b = i32::from_le(v);
Self(b)
}
}