Skip to content

Commit dd09f9a

Browse files
committed
Remove memory uninitized with safe code
1 parent 7660d49 commit dd09f9a

File tree

2 files changed

+12
-23
lines changed

2 files changed

+12
-23
lines changed

src/sys/resource.rs

Lines changed: 10 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
//! Configure the process resource limits.
22
use cfg_if::cfg_if;
3-
use std::mem;
43

54
use crate::errno::Errno;
65
use crate::Result;
@@ -131,22 +130,14 @@ cfg_if! {
131130
///
132131
/// [`Resource`]: enum.Resource.html
133132
pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)> {
134-
let mut old_rlim = mem::MaybeUninit::<rlimit>::uninit();
133+
let mut old_rlim = rlimit {
134+
rlim_cur: 0,
135+
rlim_max: 0,
136+
};
135137

136138
cfg_if! {
137139
if #[cfg(all(target_os = "linux", target_env = "gnu"))]{
138-
// the below implementation is mimicing the similar implementation in golang
139-
// https://go-review.googlesource.com/c/sys/+/230478/2/unix/syscall_linux_arm64.go#176
140-
// seems for some of the architectures, we prefer to use prlimit instead of {g,s}etrlimit
141-
142-
let res = unsafe { libc::prlimit(0, resource as __rlimit_resource_t, std::ptr::null(), old_rlim.as_mut_ptr() as *mut _) };
143-
if res == -1 {
144-
// when error happens, the map will return an Err, the (None, None) is just make compiler
145-
// happy, it will not go through
146-
return Errno::result(res).map(|_|{ (None, None) });
147-
}
148-
let res = unsafe { libc::getrlimit(resource as __rlimit_resource_t, old_rlim.as_mut_ptr() as *mut _) };
149-
140+
let res = unsafe { libc::getrlimit(resource as __rlimit_resource_t, &mut old_rlim) };
150141
}else if #[cfg(any(
151142
target_os = "freebsd",
152143
target_os = "openbsd",
@@ -158,11 +149,10 @@ pub fn getrlimit(resource: Resource) -> Result<(Option<rlim_t>, Option<rlim_t>)>
158149
target_os = "bitrig",
159150
target_os = "linux", // target_env != "gnu"
160151
))]{
161-
let res = unsafe { libc::getrlimit(resource as c_int, old_rlim.as_mut_ptr() as *mut _) };
152+
let res = unsafe { libc::getrlimit(resource as c_int, &mut old_rlim) };
162153
}
163154
}
164155

165-
let old_rlim = unsafe { old_rlim.assume_init() };
166156
Errno::result(res).map(|_| {
167157
(
168158
Some(old_rlim.rlim_cur).filter(|x| *x != RLIM_INFINITY),
@@ -204,10 +194,10 @@ pub fn setrlimit(
204194
soft_limit: Option<rlim_t>,
205195
hard_limit: Option<rlim_t>,
206196
) -> Result<()> {
207-
let mut new_rlim = unsafe { mem::MaybeUninit::<rlimit>::uninit().assume_init() };
208-
new_rlim.rlim_cur = soft_limit.unwrap_or(RLIM_INFINITY);
209-
new_rlim.rlim_max = hard_limit.unwrap_or(RLIM_INFINITY);
210-
197+
let new_rlim = rlimit {
198+
rlim_cur: soft_limit.unwrap_or(RLIM_INFINITY),
199+
rlim_max: hard_limit.unwrap_or(RLIM_INFINITY),
200+
};
211201
cfg_if! {
212202
if #[cfg(all(target_os = "linux", target_env = "gnu"))]{
213203
// the below implementation is mimicing the similar implementation in golang

test/test_resource.rs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,12 +33,11 @@ pub fn test_resource_limits_nofile() {
3333
pub fn test_resource_limits_stack() {
3434
let (mut soft_limit, hard_limit) = getrlimit(Resource::RLIMIT_STACK).unwrap();
3535
let orig_limit = (soft_limit, hard_limit);
36-
eprintln!("\n{:?} --- {:?}", soft_limit, hard_limit);
37-
soft_limit = Some(4194304);
36+
37+
soft_limit = hard_limit.or(Some(4194304));
3838
setrlimit(Resource::RLIMIT_STACK, soft_limit, hard_limit).unwrap();
3939

4040
let limit2 = getrlimit(Resource::RLIMIT_STACK).unwrap();
41-
eprintln!("\n{:?} --- {:?}", limit2.0, limit2.1);
4241

4342
assert_eq!(soft_limit, limit2.0);
4443
assert_eq!(hard_limit, limit2.1);

0 commit comments

Comments
 (0)