Skip to content

Commit 4f7f450

Browse files
committed
uefi: Use slice equality rather than memcmp
`compiler_builtins` shouldn't be called directly. Change the `PartialEq` implementation for `DevicePathNode` to use slice equality instead, which will call `memcmp`/`bcmp` via the intrinsic.
1 parent d7654b9 commit 4f7f450

File tree

1 file changed

+11
-11
lines changed

1 file changed

+11
-11
lines changed

library/std/src/sys/pal/uefi/helpers.rs

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -444,17 +444,17 @@ impl<'a> DevicePathNode<'a> {
444444

445445
impl<'a> PartialEq for DevicePathNode<'a> {
446446
fn eq(&self, other: &Self) -> bool {
447-
let self_len = self.length();
448-
let other_len = other.length();
449-
450-
self_len == other_len
451-
&& unsafe {
452-
compiler_builtins::mem::memcmp(
453-
self.protocol.as_ptr().cast(),
454-
other.protocol.as_ptr().cast(),
455-
usize::from(self_len),
456-
) == 0
457-
}
447+
// Compare as a single buffer rather than by field since it optimizes better.
448+
//
449+
// SAFETY: `Protocol` is followed by a buffer of `length - sizeof::<Protocol>()`. `Protocol`
450+
// has no padding so it is sound to interpret as a slice.
451+
unsafe {
452+
let s1 =
453+
slice::from_raw_parts(self.protocol.as_ptr().cast::<u8>(), self.length().into());
454+
let s2 =
455+
slice::from_raw_parts(other.protocol.as_ptr().cast::<u8>(), other.length().into());
456+
s1 == s2
457+
}
458458
}
459459
}
460460

0 commit comments

Comments
 (0)