Skip to content

Commit

Permalink
Remove uses of core::mem::uninitialized from ufmt.
Browse files Browse the repository at this point in the history
According to the current Rust Reference [1], storing an uninitialized `u8` is undefined behavior. This may change in the future [2], but for now we should continue to assume it is undefined behavior.

Every use of `core::mem::uninitialized` in `ufmt` is to create a local `[u8; _]`, and therefore is an example of this undefined behavior. I removed the undefined behavior in the simplest way possible, which is to replace the initializers with `[u8; _]`.

[1] https://doc.rust-lang.org/reference/behavior-considered-undefined.html
[2] rust-lang/unsafe-code-guidelines#77
  • Loading branch information
jrvanwhy committed May 20, 2021
1 parent 0fe6395 commit fc1168e
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 24 deletions.
12 changes: 6 additions & 6 deletions ufmt/src/impls/ixx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ impl uDebug for i8 {
where
W: uWrite + ?Sized,
{
let mut buf: [u8; 4] = unsafe { crate::uninitialized() };
let mut buf: [u8; 4] = [0; 4];

f.write_str(isize(isize::from(*self), &mut buf))
}
Expand All @@ -69,7 +69,7 @@ impl uDebug for i16 {
where
W: uWrite + ?Sized,
{
let mut buf: [u8; 6] = unsafe { crate::uninitialized() };
let mut buf: [u8; 6] = [0; 6];

f.write_str(isize(isize::from(*self), &mut buf))
}
Expand All @@ -90,7 +90,7 @@ impl uDebug for i32 {
where
W: uWrite + ?Sized,
{
let mut buf: [u8; 11] = unsafe { crate::uninitialized() };
let mut buf: [u8; 11] = [0; 11];

f.write_str(isize(*self as isize, &mut buf))
}
Expand All @@ -112,7 +112,7 @@ impl uDebug for i64 {
where
W: uWrite + ?Sized,
{
let mut buf: [u8; 20] = unsafe { crate::uninitialized() };
let mut buf: [u8; 20] = [0; 20];

let s = ixx!(u64, *self, buf);
f.write_str(s)
Expand All @@ -123,7 +123,7 @@ impl uDebug for i64 {
where
W: uWrite + ?Sized,
{
let mut buf: [u8; 20] = unsafe { crate::uninitialized() };
let mut buf: [u8; 20] = [0; 20];

f.write_str(isize(*self as isize, &mut buf))
}
Expand All @@ -144,7 +144,7 @@ impl uDebug for i128 {
where
W: uWrite + ?Sized,
{
let mut buf: [u8; 40] = unsafe { crate::uninitialized() };
let mut buf: [u8; 40] = [0; 40];

let s = ixx!(u128, *self, buf);
f.write_str(s)
Expand Down
2 changes: 1 addition & 1 deletion ufmt/src/impls/ptr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use crate::{uDebug, uWrite, Formatter};

macro_rules! hex {
($self:expr, $f:expr, $N:expr) => {{
let mut buf: [u8; $N] = unsafe { crate::uninitialized() };
let mut buf: [u8; $N] = [0; $N];

let i = hex(*$self as usize, &mut buf);

Expand Down
12 changes: 6 additions & 6 deletions ufmt/src/impls/uxx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ impl uDebug for u8 {
where
W: uWrite + ?Sized,
{
let mut buf: [u8; 3] = unsafe { crate::uninitialized() };
let mut buf: [u8; 3] = [0; 3];

f.write_str(usize(usize::from(*self), &mut buf))
}
Expand All @@ -53,7 +53,7 @@ impl uDebug for u16 {
where
W: uWrite + ?Sized,
{
let mut buf: [u8; 5] = unsafe { crate::uninitialized() };
let mut buf: [u8; 5] = [0; 5];

f.write_str(usize(usize::from(*self), &mut buf))
}
Expand All @@ -74,7 +74,7 @@ impl uDebug for u32 {
where
W: uWrite + ?Sized,
{
let mut buf: [u8; 10] = unsafe { crate::uninitialized() };
let mut buf: [u8; 10] = [0; 10];

f.write_str(usize(*self as usize, &mut buf))
}
Expand All @@ -96,7 +96,7 @@ impl uDebug for u64 {
where
W: uWrite + ?Sized,
{
let mut buf: [u8; 20] = unsafe { crate::uninitialized() };
let mut buf: [u8; 20] = [0; 20];

let s = uxx!(*self, buf);
f.write_str(s)
Expand All @@ -107,7 +107,7 @@ impl uDebug for u64 {
where
W: uWrite + ?Sized,
{
let mut buf: [u8; 20] = unsafe { crate::uninitialized() };
let mut buf: [u8; 20] = [0; 20];

f.write_str(usize(*self as usize, &mut buf))
}
Expand All @@ -128,7 +128,7 @@ impl uDebug for u128 {
where
W: uWrite + ?Sized,
{
let mut buf: [u8; 39] = unsafe { crate::uninitialized() };
let mut buf: [u8; 39] = [0; 39];

let s = uxx!(*self, buf);
f.write_str(s)
Expand Down
5 changes: 0 additions & 5 deletions ufmt/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,11 +248,6 @@ pub mod derive {
pub use ufmt_macros::uDebug;
}

#[allow(deprecated)]
unsafe fn uninitialized<T>() -> T {
core::mem::uninitialized()
}

/// Just like `core::fmt::Debug`
#[allow(non_camel_case_types)]
pub trait uDebug {
Expand Down
7 changes: 1 addition & 6 deletions ufmt/write/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,6 @@
#[cfg(feature = "std")]
use core::convert::Infallible;

#[allow(deprecated)]
unsafe fn uninitialized<T>() -> T {
core::mem::uninitialized()
}

/// A collection of methods that are required / used to format a message into a stream.
#[allow(non_camel_case_types)]
pub trait uWrite {
Expand All @@ -32,7 +27,7 @@ pub trait uWrite {
/// entire byte sequence was successfully written, and this method will not return until all
/// data has been written or an error occurs.
fn write_char(&mut self, c: char) -> Result<(), Self::Error> {
let mut buf: [u8; 4] = unsafe { uninitialized() };
let mut buf: [u8; 4] = [0; 4];
self.write_str(c.encode_utf8(&mut buf))
}
}
Expand Down

0 comments on commit fc1168e

Please sign in to comment.