mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-18 08:52:45 +00:00
flatipc: add more checks to vec and string
These are required as this goes across the IPC boundary. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
0c5b011fcf
commit
3eb54d2cc9
2 changed files with 12 additions and 3 deletions
|
|
@ -23,6 +23,7 @@ impl<const N: usize> From<&str> for String<N> {
|
||||||
|
|
||||||
impl<const N: usize> core::fmt::Write for String<N> {
|
impl<const N: usize> core::fmt::Write for String<N> {
|
||||||
fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
||||||
|
assert!(self.length <= self.buffer.len());
|
||||||
if s.len() + self.length > N {
|
if s.len() + self.length > N {
|
||||||
return Err(core::fmt::Error);
|
return Err(core::fmt::Error);
|
||||||
}
|
}
|
||||||
|
|
@ -52,7 +53,7 @@ impl<const N: usize> core::fmt::Display for String<N> {
|
||||||
|
|
||||||
impl<const N: usize> AsRef<str> for String<N> {
|
impl<const N: usize> AsRef<str> for String<N> {
|
||||||
fn as_ref(&self) -> &str {
|
fn as_ref(&self) -> &str {
|
||||||
// Safe because we guarantee the buffer is valid UTF-8
|
assert!(self.length <= self.buffer.len());
|
||||||
unsafe { core::str::from_utf8_unchecked(&self.buffer[0..self.length]) }
|
core::str::from_utf8(&self.buffer[0..self.length]).unwrap()
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -21,6 +21,7 @@ impl<T, const N: usize> Vec<T, N> {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn pop(&mut self) -> Option<T> {
|
pub fn pop(&mut self) -> Option<T> {
|
||||||
|
assert!(self.length <= self.buffer.len());
|
||||||
if self.length > 0 {
|
if self.length > 0 {
|
||||||
self.length -= 1;
|
self.length -= 1;
|
||||||
unsafe { Some(self.buffer[self.length].as_ptr().read()) }
|
unsafe { Some(self.buffer[self.length].as_ptr().read()) }
|
||||||
|
|
@ -29,17 +30,23 @@ impl<T, const N: usize> Vec<T, N> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn len(&self) -> usize { self.length }
|
pub fn len(&self) -> usize {
|
||||||
|
assert!(self.length <= self.buffer.len());
|
||||||
|
self.length
|
||||||
|
}
|
||||||
|
|
||||||
pub fn as_slice(&self) -> &[T] {
|
pub fn as_slice(&self) -> &[T] {
|
||||||
|
assert!(self.length <= self.buffer.len());
|
||||||
unsafe { core::slice::from_raw_parts(self.buffer.as_ptr() as *const T, self.length) }
|
unsafe { core::slice::from_raw_parts(self.buffer.as_ptr() as *const T, self.length) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn as_mut_slice(&mut self) -> &mut [T] {
|
pub fn as_mut_slice(&mut self) -> &mut [T] {
|
||||||
|
assert!(self.length <= self.buffer.len());
|
||||||
unsafe { core::slice::from_raw_parts_mut(self.buffer.as_mut_ptr() as *mut T, self.length) }
|
unsafe { core::slice::from_raw_parts_mut(self.buffer.as_mut_ptr() as *mut T, self.length) }
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn clear(&mut self) {
|
pub fn clear(&mut self) {
|
||||||
|
assert!(self.length <= self.buffer.len());
|
||||||
for i in 0..self.length {
|
for i in 0..self.length {
|
||||||
unsafe {
|
unsafe {
|
||||||
core::ptr::drop_in_place(&mut self.buffer[i]);
|
core::ptr::drop_in_place(&mut self.buffer[i]);
|
||||||
|
|
@ -56,6 +63,7 @@ impl<T, const N: usize> Vec<T, N> {
|
||||||
where
|
where
|
||||||
T: Clone,
|
T: Clone,
|
||||||
{
|
{
|
||||||
|
assert!(self.length <= self.buffer.len());
|
||||||
if new_len > self.length {
|
if new_len > self.length {
|
||||||
for _ in self.length..new_len {
|
for _ in self.length..new_len {
|
||||||
self.push(value.clone());
|
self.push(value.clone());
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue