mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +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> {
|
||||
fn write_str(&mut self, s: &str) -> core::fmt::Result {
|
||||
assert!(self.length <= self.buffer.len());
|
||||
if s.len() + self.length > N {
|
||||
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> {
|
||||
fn as_ref(&self) -> &str {
|
||||
// Safe because we guarantee the buffer is valid UTF-8
|
||||
unsafe { core::str::from_utf8_unchecked(&self.buffer[0..self.length]) }
|
||||
assert!(self.length <= self.buffer.len());
|
||||
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> {
|
||||
assert!(self.length <= self.buffer.len());
|
||||
if self.length > 0 {
|
||||
self.length -= 1;
|
||||
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] {
|
||||
assert!(self.length <= self.buffer.len());
|
||||
unsafe { core::slice::from_raw_parts(self.buffer.as_ptr() as *const T, self.length) }
|
||||
}
|
||||
|
||||
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) }
|
||||
}
|
||||
|
||||
pub fn clear(&mut self) {
|
||||
assert!(self.length <= self.buffer.len());
|
||||
for i in 0..self.length {
|
||||
unsafe {
|
||||
core::ptr::drop_in_place(&mut self.buffer[i]);
|
||||
|
|
@ -56,6 +63,7 @@ impl<T, const N: usize> Vec<T, N> {
|
|||
where
|
||||
T: Clone,
|
||||
{
|
||||
assert!(self.length <= self.buffer.len());
|
||||
if new_len > self.length {
|
||||
for _ in self.length..new_len {
|
||||
self.push(value.clone());
|
||||
|
|
|
|||
Loading…
Reference in a new issue