Skip to content

Commit c153cd1

Browse files
bors[bot]jrvanwhy
andauthored
Merge #306
306: Remove uses of `core::mem::uninitialized` from `ufmt`. r=jrvanwhy a=jrvanwhy 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 Co-authored-by: Johnathan Van Why <jrvanwhy@google.com>
2 parents 0fe6395 + fc1168e commit c153cd1

File tree

5 files changed

+14
-24
lines changed

5 files changed

+14
-24
lines changed

ufmt/src/impls/ixx.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ impl uDebug for i8 {
4848
where
4949
W: uWrite + ?Sized,
5050
{
51-
let mut buf: [u8; 4] = unsafe { crate::uninitialized() };
51+
let mut buf: [u8; 4] = [0; 4];
5252

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

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

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

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

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

149149
let s = ixx!(u128, *self, buf);
150150
f.write_str(s)

ufmt/src/impls/ptr.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use crate::{uDebug, uWrite, Formatter};
44

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

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

ufmt/src/impls/uxx.rs

+6-6
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ impl uDebug for u8 {
3232
where
3333
W: uWrite + ?Sized,
3434
{
35-
let mut buf: [u8; 3] = unsafe { crate::uninitialized() };
35+
let mut buf: [u8; 3] = [0; 3];
3636

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

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

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

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

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

133133
let s = uxx!(*self, buf);
134134
f.write_str(s)

ufmt/src/lib.rs

-5
Original file line numberDiff line numberDiff line change
@@ -248,11 +248,6 @@ pub mod derive {
248248
pub use ufmt_macros::uDebug;
249249
}
250250

251-
#[allow(deprecated)]
252-
unsafe fn uninitialized<T>() -> T {
253-
core::mem::uninitialized()
254-
}
255-
256251
/// Just like `core::fmt::Debug`
257252
#[allow(non_camel_case_types)]
258253
pub trait uDebug {

ufmt/write/src/lib.rs

+1-6
Original file line numberDiff line numberDiff line change
@@ -9,11 +9,6 @@
99
#[cfg(feature = "std")]
1010
use core::convert::Infallible;
1111

12-
#[allow(deprecated)]
13-
unsafe fn uninitialized<T>() -> T {
14-
core::mem::uninitialized()
15-
}
16-
1712
/// A collection of methods that are required / used to format a message into a stream.
1813
#[allow(non_camel_case_types)]
1914
pub trait uWrite {
@@ -32,7 +27,7 @@ pub trait uWrite {
3227
/// entire byte sequence was successfully written, and this method will not return until all
3328
/// data has been written or an error occurs.
3429
fn write_char(&mut self, c: char) -> Result<(), Self::Error> {
35-
let mut buf: [u8; 4] = unsafe { uninitialized() };
30+
let mut buf: [u8; 4] = [0; 4];
3631
self.write_str(c.encode_utf8(&mut buf))
3732
}
3833
}

0 commit comments

Comments
 (0)