Skip to content

Commit b232247

Browse files
committed
rust: kernel: sysctl: do not use vec!
`vec!` may allocate, which in turn requires the `exchange_malloc` `lang_item`. For some reason, in 1.56.0 this compiled. Signed-off-by: Miguel Ojeda <[email protected]>
1 parent cb94e38 commit b232247

File tree

1 file changed

+16
-17
lines changed

1 file changed

+16
-17
lines changed

rust/kernel/sysctl.rs

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
//! Reference: <https://www.kernel.org/doc/Documentation/sysctl/README>
88
99
use alloc::boxed::Box;
10-
use alloc::vec;
10+
use alloc::vec::Vec;
1111
use core::mem;
1212
use core::ptr;
1313
use core::sync::atomic;
@@ -141,22 +141,21 @@ impl<T: SysctlStorage> Sysctl<T> {
141141
}
142142

143143
let storage = Box::try_new(storage)?;
144-
let mut table = vec![
145-
bindings::ctl_table {
146-
procname: name.as_char_ptr(),
147-
mode: mode.as_int(),
148-
data: &*storage as *const T as *mut c_types::c_void,
149-
proc_handler: Some(proc_handler::<T>),
150-
151-
maxlen: 0,
152-
child: ptr::null_mut(),
153-
poll: ptr::null_mut(),
154-
extra1: ptr::null_mut(),
155-
extra2: ptr::null_mut(),
156-
},
157-
unsafe { mem::zeroed() },
158-
]
159-
.try_into_boxed_slice()?;
144+
let mut table = Vec::try_with_capacity(2)?;
145+
table.try_push(bindings::ctl_table {
146+
procname: name.as_char_ptr(),
147+
mode: mode.as_int(),
148+
data: &*storage as *const T as *mut c_types::c_void,
149+
proc_handler: Some(proc_handler::<T>),
150+
151+
maxlen: 0,
152+
child: ptr::null_mut(),
153+
poll: ptr::null_mut(),
154+
extra1: ptr::null_mut(),
155+
extra2: ptr::null_mut(),
156+
})?;
157+
table.try_push(unsafe { mem::zeroed() })?;
158+
let mut table = table.try_into_boxed_slice()?;
160159

161160
let result = unsafe { bindings::register_sysctl(path.as_char_ptr(), table.as_mut_ptr()) };
162161
if result.is_null() {

0 commit comments

Comments
 (0)