From 884ec8583839c864b4abc87eb8f23a53f14b3506 Mon Sep 17 00:00:00 2001
From: AdminXVII <xavier.lheureux@icloud.com>
Date: Mon, 20 Jan 2020 16:54:22 +0000
Subject: [PATCH] Replace occurences of uninitialized with MaybeUninit

mem::uninitialized is deprecated, so move over the not-UB MaybeUninit.
---
 src/header/netdb/host.rs  |  5 +++--
 src/header/signal/mod.rs  | 12 ++++++------
 src/header/stdlib/mod.rs  |  8 ++++----
 src/header/stdlib/sort.rs |  9 ++++-----
 src/header/time/mod.rs    |  5 +++--
 src/header/unistd/mod.rs  | 10 ++++++----
 6 files changed, 26 insertions(+), 23 deletions(-)

diff --git a/src/header/netdb/host.rs b/src/header/netdb/host.rs
index e22a4ec6..1b4ba101 100644
--- a/src/header/netdb/host.rs
+++ b/src/header/netdb/host.rs
@@ -80,8 +80,9 @@ pub unsafe extern "C" fn gethostent() -> *mut hostent {
     let mut addr_vec = iter.next().unwrap().as_bytes().to_vec();
     addr_vec.push(b'\0');
     let addr_cstr = addr_vec.as_slice().as_ptr() as *const i8;
-    let mut addr = mem::uninitialized();
-    inet_aton(addr_cstr, &mut addr);
+    let mut addr = mem::MaybeUninit::uninit();
+    inet_aton(addr_cstr, addr.as_mut_ptr());
+    let addr = addr.assume_init();
 
     _HOST_ADDR_LIST = mem::transmute::<u32, [u8; 4]>(addr.s_addr);
     HOST_ADDR_LIST = [_HOST_ADDR_LIST.as_mut_ptr() as *mut c_char, ptr::null_mut()];
diff --git a/src/header/signal/mod.rs b/src/header/signal/mod.rs
index bf36e6d9..c7868326 100644
--- a/src/header/signal/mod.rs
+++ b/src/header/signal/mod.rs
@@ -21,9 +21,9 @@ pub mod sys;
 
 type SigSet = BitSet<[c_ulong; 1]>;
 
-pub(crate) const SIG_DFL: usize = 0;
-pub(crate) const SIG_IGN: usize = 1;
-pub(crate) const SIG_ERR: isize = -1;
+pub const SIG_DFL: usize = 0;
+pub const SIG_IGN: usize = 1;
+pub const SIG_ERR: isize = -1;
 
 pub const SIG_BLOCK: c_int = 0;
 pub const SIG_UNBLOCK: c_int = 1;
@@ -202,12 +202,12 @@ pub extern "C" fn signal(
         sa_restorer: Some(__restore_rt),
         sa_mask: sigset_t::default(),
     };
-    let mut old_sa = unsafe { mem::uninitialized() };
-    if unsafe { sigaction(sig, &sa, &mut old_sa) } < 0 {
+    let mut old_sa = mem::MaybeUninit::uninit();
+    if unsafe { sigaction(sig, &sa, old_sa.as_mut_ptr()) } < 0 {
         mem::forget(old_sa);
         return unsafe { mem::transmute(SIG_ERR) };
     }
-    old_sa.sa_handler
+    unsafe { old_sa.assume_init() }.sa_handler
 }
 
 // #[no_mangle]
diff --git a/src/header/stdlib/mod.rs b/src/header/stdlib/mod.rs
index 85e53e46..484127dc 100644
--- a/src/header/stdlib/mod.rs
+++ b/src/header/stdlib/mod.rs
@@ -17,7 +17,7 @@ use crate::{
         fcntl::*,
         limits,
         string::*,
-        time::{constants::CLOCK_MONOTONIC, timespec},
+        time::constants::CLOCK_MONOTONIC,
         unistd::{self, sysconf, _SC_PAGESIZE},
         wchar::*,
     },
@@ -589,9 +589,9 @@ pub unsafe extern "C" fn mktemp(name: *mut c_char) -> *mut c_char {
 }
 
 fn get_nstime() -> u64 {
-    let mut ts: timespec = unsafe { mem::uninitialized() };
-    Sys::clock_gettime(CLOCK_MONOTONIC, &mut ts);
-    ts.tv_nsec as u64
+    let mut ts = mem::MaybeUninit::uninit();
+    Sys::clock_gettime(CLOCK_MONOTONIC, ts.as_mut_ptr());
+    unsafe { ts.assume_init() }.tv_nsec as u64
 }
 
 #[no_mangle]
diff --git a/src/header/stdlib/sort.rs b/src/header/stdlib/sort.rs
index 60506d6d..5a0cd5fa 100644
--- a/src/header/stdlib/sort.rs
+++ b/src/header/stdlib/sort.rs
@@ -224,16 +224,15 @@ fn swap(mut ptr1: *mut c_char, mut ptr2: *mut c_char, mut width: size_t) {
 
     const BUFSIZE: usize = 128;
 
-    let mut buffer: [c_char; BUFSIZE] = unsafe { mem::uninitialized() };
+    let mut buffer = mem::MaybeUninit::<[c_char; BUFSIZE]>::uninit();
     while width > 0 {
         let copy_size = BUFSIZE.min(width as usize);
+        let buf = buffer.as_mut_ptr() as *mut c_char;
 
         unsafe {
-            buffer
-                .as_mut_ptr()
-                .copy_from_nonoverlapping(ptr1, copy_size);
+            buf.copy_from_nonoverlapping(ptr1, copy_size);
             ptr1.copy_from_nonoverlapping(ptr2, copy_size);
-            ptr2.copy_from_nonoverlapping(buffer.as_ptr(), copy_size);
+            ptr2.copy_from_nonoverlapping(buf, copy_size);
 
             ptr1 = ptr1.add(copy_size);
             ptr2 = ptr2.add(copy_size);
diff --git a/src/header/time/mod.rs b/src/header/time/mod.rs
index 1d7b5a18..087aa090 100644
--- a/src/header/time/mod.rs
+++ b/src/header/time/mod.rs
@@ -102,11 +102,12 @@ pub unsafe extern "C" fn asctime_r(tm: *const tm, buf: *mut c_char) -> *mut c_ch
 
 #[no_mangle]
 pub extern "C" fn clock() -> clock_t {
-    let mut ts: timespec = unsafe { core::mem::uninitialized() };
+    let mut ts = core::mem::MaybeUninit::<timespec>::uninit();
 
-    if clock_gettime(CLOCK_PROCESS_CPUTIME_ID, &mut ts) != 0 {
+    if clock_gettime(CLOCK_PROCESS_CPUTIME_ID, ts.as_mut_ptr()) != 0 {
         return -1;
     }
+    let ts = unsafe { ts.assume_init() };
 
     if ts.tv_sec > time_t::max_value() / CLOCKS_PER_SEC
         || ts.tv_nsec / (1_000_000_000 / CLOCKS_PER_SEC)
diff --git a/src/header/unistd/mod.rs b/src/header/unistd/mod.rs
index b64997d0..f417e5bb 100644
--- a/src/header/unistd/mod.rs
+++ b/src/header/unistd/mod.rs
@@ -4,7 +4,9 @@ use core::{convert::TryFrom, mem, ptr, slice};
 
 use crate::{
     c_str::CStr,
-    header::{errno, limits, stdlib::getenv, sys_ioctl, sys_time, termios, time::timespec},
+    header::{
+        errno, limits, stdlib::getenv, sys_ioctl, sys_time, sys_utsname, termios, time::timespec,
+    },
     platform::{self, types::*, Pal, Sys},
 };
 use alloc::collections::LinkedList;
@@ -309,13 +311,13 @@ pub extern "C" fn gethostid() -> c_long {
 
 #[no_mangle]
 pub unsafe extern "C" fn gethostname(mut name: *mut c_char, mut len: size_t) -> c_int {
-    let mut uts = mem::uninitialized();
-    let err = Sys::uname(&mut uts);
+    let mut uts = mem::MaybeUninit::<sys_utsname::utsname>::uninit();
+    let err = Sys::uname(uts.as_mut_ptr());
     if err < 0 {
         mem::forget(uts);
         return err;
     }
-    for c in uts.nodename.iter() {
+    for c in uts.assume_init().nodename.iter() {
         if len == 0 {
             break;
         }
-- 
GitLab