diff --git a/library/std/src/lib.rs b/library/std/src/lib.rs index 2e458562744bf..9442f7da43c4b 100644 --- a/library/std/src/lib.rs +++ b/library/std/src/lib.rs @@ -357,6 +357,7 @@ #![feature(slice_concat_trait)] #![feature(thin_box)] #![feature(try_reserve_kind)] +#![feature(try_with_capacity)] #![feature(vec_into_raw_parts)] // tidy-alphabetical-end // diff --git a/library/std/src/sys/pal/uefi/helpers.rs b/library/std/src/sys/pal/uefi/helpers.rs index 9837cc89f2d39..09d7c944020b4 100644 --- a/library/std/src/sys/pal/uefi/helpers.rs +++ b/library/std/src/sys/pal/uefi/helpers.rs @@ -62,7 +62,8 @@ pub(crate) fn locate_handles(mut guid: Guid) -> io::Result(), 0); let num_of_handles = buf_len / size_of::(); - let mut buf: Vec = Vec::with_capacity(num_of_handles); + let mut buf: Vec = + Vec::try_with_capacity(num_of_handles).map_err(|_| io::ErrorKind::OutOfMemory)?; match inner(&mut guid, boot_services, &mut buf_len, buf.as_mut_ptr()) { Ok(()) => { // This is safe because the call will succeed only if buf_len >= required length. diff --git a/library/std/src/sys/pal/unix/os.rs b/library/std/src/sys/pal/unix/os.rs index 881b3a25c5152..4850e1131c0ff 100644 --- a/library/std/src/sys/pal/unix/os.rs +++ b/library/std/src/sys/pal/unix/os.rs @@ -154,7 +154,7 @@ pub fn getcwd() -> io::Result { #[cfg(not(target_os = "espidf"))] pub fn getcwd() -> io::Result { - let mut buf = Vec::with_capacity(512); + let mut buf = Vec::try_with_capacity(512).map_err(|_| io::ErrorKind::OutOfMemory)?; loop { unsafe { let ptr = buf.as_mut_ptr() as *mut libc::c_char; @@ -352,7 +352,8 @@ pub fn current_exe() -> io::Result { "KERN_PROC_PATHNAME sysctl returned zero-length string", )); } - let mut path: Vec = Vec::with_capacity(path_len); + let mut path: Vec = + Vec::try_with_capacity(path_len).map_err(|_| io::ErrorKind::OutOfMemory)?; cvt(libc::sysctl( mib.as_ptr(), mib.len() as libc::c_uint, @@ -438,7 +439,8 @@ pub fn current_exe() -> io::Result { if sz == 0 { return Err(io::Error::last_os_error()); } - let mut v: Vec = Vec::with_capacity(sz as usize); + let mut v: Vec = + Vec::try_with_capacity(sz as usize).map_err(|_| io::ErrorKind::OutOfMemory)?; let err = libc::_NSGetExecutablePath(v.as_mut_ptr() as *mut i8, &mut sz); if err != 0 { return Err(io::Error::last_os_error()); @@ -726,7 +728,7 @@ pub fn home_dir() -> Option { n if n < 0 => 512 as usize, n => n as usize, }; - let mut buf = Vec::with_capacity(amt); + let mut buf = Vec::try_with_capacity(amt).ok()?; let mut passwd: libc::passwd = mem::zeroed(); let mut result = ptr::null_mut(); match libc::getpwuid_r( diff --git a/library/std/src/sys/pal/unix/thread.rs b/library/std/src/sys/pal/unix/thread.rs index 7e4a01a5ecd33..361073561ec41 100644 --- a/library/std/src/sys/pal/unix/thread.rs +++ b/library/std/src/sys/pal/unix/thread.rs @@ -498,7 +498,7 @@ mod cgroups { } let _: Option<()> = try { - let mut buf = Vec::with_capacity(128); + let mut buf = Vec::try_with_capacity(128).ok()?; // find our place in the cgroup hierarchy File::open("/proc/self/cgroup").ok()?.read_to_end(&mut buf).ok()?; let (cgroup_path, version) = diff --git a/library/std/src/sys/pal/wasi/os.rs b/library/std/src/sys/pal/wasi/os.rs index 530d360217216..3ce249d2a9efc 100644 --- a/library/std/src/sys/pal/wasi/os.rs +++ b/library/std/src/sys/pal/wasi/os.rs @@ -69,7 +69,7 @@ pub fn error_string(errno: i32) -> String { } pub fn getcwd() -> io::Result { - let mut buf = Vec::with_capacity(512); + let mut buf = Vec::try_with_capacity(512).map_err(|_| io::ErrorKind::OutOfMemory)?; loop { unsafe { let ptr = buf.as_mut_ptr() as *mut libc::c_char; diff --git a/library/std/src/sys/pal/windows/mod.rs b/library/std/src/sys/pal/windows/mod.rs index 364521dba40ac..78490197bc59b 100644 --- a/library/std/src/sys/pal/windows/mod.rs +++ b/library/std/src/sys/pal/windows/mod.rs @@ -181,7 +181,8 @@ pub fn to_u16s>(s: S) -> crate::io::Result> { // in the OsStr plus one for the null-terminating character. We are not // wasting bytes here as paths created by this function are primarily used // in an ephemeral fashion. - let mut maybe_result = Vec::with_capacity(s.len() + 1); + let mut maybe_result = + Vec::try_with_capacity(s.len() + 1).map_err(|_| ErrorKind::OutOfMemory)?; maybe_result.extend(s.encode_wide()); if unrolled_find_u16s(0, &maybe_result).is_some() {