Skip to contentSkip to conversation

rust: netlink: remove unnecessary bitfield transmutes

PATCH Applied Thorsten Blum 2 participants 2 messages Nov 2 – Nov 9

Subsystems (3)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 rust: netlink: remove unnecessary bitfield transmutes

Use __BindgenBitfieldUnit::new() to build the bitfield.

Since the generated type implements PartialEq, compare the bitfields directly in test_family_flags_bitfield().

---
test_family_flags_bitfield() still passes.

 rust/kernel/net/netlink.rs | 9 ++-------
 1 file changed, 2 insertions(+), 7 deletions(-)
rust/kernel/net/netlink.rs +2 -7
index 2f8733a694a9..d0c79a254f12 100644
@@ -180,8 +180,7 @@ impl FamilyFlags {
         }
         // Convert from little endian to the target's endianness.
         bits = u8::from_le(bits);
-        // SAFETY: This bitfield is represented as an u8.
-        unsafe { core::mem::transmute::<u8, bindings::__BindgenBitfieldUnit<[u8; 1]>>(bits) }
+        bindings::__BindgenBitfieldUnit::new([bits])
     }
 }
 
@@ -335,11 +334,7 @@ fn test_family_flags_bitfield() {
                 }
                 .into_bitfield();
 
-                // SAFETY: The bit field is stored as u8.
-                let b_val: u8 = unsafe { core::mem::transmute(b_fam._bitfield_1) };
-                // SAFETY: The bit field is stored as u8.
-                let c_val: u8 = unsafe { core::mem::transmute(c_bitfield) };
-                assert_eq!(b_val, c_val);
+                assert_eq!(b_fam._bitfield_1, c_bitfield);
             }
         }
     }
#1 lore raw
This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Hello:

This patch was applied to netdev/net-next.git (main) by Jakub Kicinski <[email protected]>:

Quoted 8 lines · On Mon, 2 Nov 2026 03:33:03 +0100 you wrote:

Use __BindgenBitfieldUnit::new() to build the bitfield.

Since the generated type implements PartialEq, compare the bitfields directly in test_family_flags_bitfield().

[...]

Here is the summary with links:
- [net-next] rust: netlink: remove unnecessary bitfield transmutes

    https://git.kernel.org/netdev/net-next/c/ce8659b5c8ac

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html