mirror of
https://github.com/saymrwulf/xous-core.git
synced 2026-11-15 08:30:02 +00:00
shell: initialize the logstr before using it
This fixes UB when accessing an uninitialized value. Signed-off-by: Sean Cross <sean@xobs.io>
This commit is contained in:
parent
ffcccab513
commit
dfed13bd9f
1 changed files with 13 additions and 11 deletions
|
|
@ -17,13 +17,22 @@ impl<'a> LogStr<'a> {
|
|||
)
|
||||
.expect("couldn't allocate memory");
|
||||
|
||||
let raw_slice = unsafe { slice::from_raw_parts_mut(mem.as_ptr() as *mut u8, 4096) };
|
||||
// Zero out the buffer. This is required to avoid UB when accessing
|
||||
// "uninitialized" memory.
|
||||
unsafe {
|
||||
let ptr = mem.as_mut_ptr();
|
||||
for off in 0..mem.len() {
|
||||
ptr.add(off).write_volatile(0);
|
||||
}
|
||||
}
|
||||
|
||||
let raw_slice = unsafe { slice::from_raw_parts_mut(mem.as_mut_ptr(), mem.len()) };
|
||||
|
||||
LogStr {
|
||||
raw_slice,
|
||||
len: 0,
|
||||
string: unsafe {
|
||||
core::str::from_utf8_unchecked(slice::from_raw_parts(mem.as_ptr() as *mut u8, 0))
|
||||
core::str::from_utf8_unchecked(slice::from_raw_parts(mem.as_ptr(), 0))
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
@ -40,13 +49,6 @@ impl<'a> LogStr<'a> {
|
|||
}
|
||||
|
||||
pub fn as_memory_message(&self, id: xous::MessageId) -> Result<xous::MemoryMessage, xous::Error> {
|
||||
|
||||
print!("SHELL: String @ {:08x}:", self.raw_slice.as_ptr() as usize);
|
||||
for offset in 0..self.len {
|
||||
print!(" {:02x}", self.raw_slice[offset]);
|
||||
}
|
||||
println!(" (length: {})", self.len);
|
||||
|
||||
Ok(xous::MemoryMessage {
|
||||
id,
|
||||
buf: xous::MemoryRange::new(self.raw_slice.as_ptr() as usize, self.raw_slice.len()).unwrap(),
|
||||
|
|
@ -67,8 +69,8 @@ impl<'a> LogStr<'a> {
|
|||
|
||||
impl<'a> Write for LogStr<'a> {
|
||||
fn write_str(&mut self, s: &str) -> Result<(), Error> {
|
||||
for c in s.bytes() {
|
||||
self.raw_slice[self.len] = c;
|
||||
for (dest, src) in self.raw_slice.iter_mut().skip(self.len).zip(s.bytes()) {
|
||||
*dest = src;
|
||||
self.len += 1;
|
||||
}
|
||||
self.string = unsafe { core::str::from_utf8_unchecked(slice::from_raw_parts(self.raw_slice.as_ptr(), self.len)) };
|
||||
|
|
|
|||
Loading…
Reference in a new issue