Skip to content

Commit 92fd540

Browse files
y86-devojeda
authored andcommitted
rust: init: make initializer values inaccessible after initializing
Previously the init macros would create a local variable with the name and hygiene of the field that is being initialized to store the value of the field. This would override any user defined variables. For example: ``` struct Foo { a: usize, b: usize, } let a = 10; let foo = init!(Foo{ a: a + 1, // This creates a local variable named `a`. b: a, // This refers to that variable! }); let foo = Box::init!(foo)?; assert_eq!(foo.a, 11); assert_eq!(foo.b, 11); ``` This patch changes this behavior, so the above code would panic at the last assertion, since `b` would have value 10. Reviewed-by: Martin Rodriguez Reboredo <[email protected]> Reviewed-by: Alice Ryhl <[email protected]> Reviewed-by: Gary Guo <[email protected]> Signed-off-by: Benno Lossin <[email protected]> Link: https://lore.kernel.org/r/[email protected] Signed-off-by: Miguel Ojeda <[email protected]>
1 parent b9b88be commit 92fd540

File tree

1 file changed

+11
-9
lines changed

1 file changed

+11
-9
lines changed

rust/kernel/init/macros.rs

+11-9
Original file line numberDiff line numberDiff line change
@@ -1075,13 +1075,13 @@ macro_rules! __init_internal {
10751075
// In-place initialization syntax.
10761076
@munch_fields($field:ident <- $val:expr, $($rest:tt)*),
10771077
) => {
1078-
let $field = $val;
1078+
let init = $val;
10791079
// Call the initializer.
10801080
//
10811081
// SAFETY: `slot` is valid, because we are inside of an initializer closure, we
10821082
// return when an error/panic occurs.
10831083
// We also use the `data` to require the correct trait (`Init` or `PinInit`) for `$field`.
1084-
unsafe { $data.$field(::core::ptr::addr_of_mut!((*$slot).$field), $field)? };
1084+
unsafe { $data.$field(::core::ptr::addr_of_mut!((*$slot).$field), init)? };
10851085
// Create the drop guard:
10861086
//
10871087
// We rely on macro hygiene to make it impossible for users to access this local variable.
@@ -1107,12 +1107,12 @@ macro_rules! __init_internal {
11071107
// In-place initialization syntax.
11081108
@munch_fields($field:ident <- $val:expr, $($rest:tt)*),
11091109
) => {
1110-
let $field = $val;
1110+
let init = $val;
11111111
// Call the initializer.
11121112
//
11131113
// SAFETY: `slot` is valid, because we are inside of an initializer closure, we
11141114
// return when an error/panic occurs.
1115-
unsafe { $crate::init::Init::__init($field, ::core::ptr::addr_of_mut!((*$slot).$field))? };
1115+
unsafe { $crate::init::Init::__init(init, ::core::ptr::addr_of_mut!((*$slot).$field))? };
11161116
// Create the drop guard:
11171117
//
11181118
// We rely on macro hygiene to make it impossible for users to access this local variable.
@@ -1138,11 +1138,13 @@ macro_rules! __init_internal {
11381138
// Init by-value.
11391139
@munch_fields($field:ident $(: $val:expr)?, $($rest:tt)*),
11401140
) => {
1141-
$(let $field = $val;)?
1142-
// Initialize the field.
1143-
//
1144-
// SAFETY: The memory at `slot` is uninitialized.
1145-
unsafe { ::core::ptr::write(::core::ptr::addr_of_mut!((*$slot).$field), $field) };
1141+
{
1142+
$(let $field = $val;)?
1143+
// Initialize the field.
1144+
//
1145+
// SAFETY: The memory at `slot` is uninitialized.
1146+
unsafe { ::core::ptr::write(::core::ptr::addr_of_mut!((*$slot).$field), $field) };
1147+
}
11461148
// Create the drop guard:
11471149
//
11481150
// We rely on macro hygiene to make it impossible for users to access this local variable.

0 commit comments

Comments
 (0)