mirror of
https://github.com/ibm-s390-linux/s390-tools.git
synced 2026-08-05 02:14:52 +00:00
rust/pv: Remove memeq function
The rust std lib already provides functionality to compare two slies. Replace all `memeq` invocations with == and remove the `memeq` function. As a side effect this eliminates some unsafe code in this crate. Reviewed-by: Marc Hartmayer <mhartmay@linux.ibm.com> Signed-off-by: Steffen Eiden <seiden@linux.ibm.com> Signed-off-by: Jan Höppner <hoeppner@linux.ibm.com>
This commit is contained in:
committed by
Jan Höppner
parent
c70477f8c6
commit
b71279cda5
@@ -3,7 +3,7 @@
|
||||
// Copyright IBM Corp. 2023
|
||||
|
||||
use crate::error::bail_hkd_verify;
|
||||
use crate::misc::{memeq, read_crls};
|
||||
use crate::misc::read_crls;
|
||||
use crate::HkdVerifyErrorType::*;
|
||||
use crate::{Error, Result};
|
||||
use curl::easy::{Easy2, Handler, WriteError};
|
||||
@@ -219,7 +219,7 @@ fn name_data_eq(entries: &X509NameRef, nid: Nid, rhs: &[u8]) -> bool {
|
||||
let mut it = entries.entries_by_nid(nid);
|
||||
match it.next() {
|
||||
None => false,
|
||||
Some(entry) => memeq(entry.data().as_slice(), rhs),
|
||||
Some(entry) => entry.data().as_slice() == rhs,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -376,11 +376,12 @@ fn check_x509_name_equal(lhs: &X509NameRef, rhs: &X509NameRef) -> Result<()> {
|
||||
}
|
||||
|
||||
for l in lhs.entries() {
|
||||
let ldata = l.data().as_slice();
|
||||
|
||||
// search for the matching value in the rhs names
|
||||
// found none? -> names are not equal
|
||||
if !rhs.entries().any(|r| memeq(ldata, r.data().as_slice())) {
|
||||
if !rhs
|
||||
.entries()
|
||||
.any(|r| l.data().as_slice() == r.data().as_slice())
|
||||
{
|
||||
bail_hkd_verify!(IssuerMismatch);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -26,7 +26,7 @@ pub use error::{Error, FileAccessErrorType, FileIoErrorType, Result};
|
||||
pub mod misc {
|
||||
pub use crate::utils::pv_guest_bit_set;
|
||||
pub use crate::utils::{create_file, open_file, read_exact_file, read_file, write_file};
|
||||
pub use crate::utils::{memeq, parse_hex, to_u16, to_u32, try_parse_u128, try_parse_u64};
|
||||
pub use crate::utils::{parse_hex, to_u16, to_u32, try_parse_u128, try_parse_u64};
|
||||
pub use crate::utils::{read, write};
|
||||
pub use crate::utils::{Flags, Lsb0Flags64, Msb0Flags64};
|
||||
}
|
||||
@@ -65,7 +65,7 @@ pub mod request {
|
||||
if v.len() < Self::MAGIC.len() {
|
||||
return false;
|
||||
}
|
||||
crate::misc::memeq(&v[..Self::MAGIC.len()], &Self::MAGIC)
|
||||
v[..Self::MAGIC.len()] == Self::MAGIC
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -335,20 +335,6 @@ usize_to_ui! {
|
||||
#[doc = r"u16"]
|
||||
=> u16, to_u16}
|
||||
|
||||
/// Test if both slices contain the exact same bytes.
|
||||
///
|
||||
/// Do not use this to compare cryptographic values (i.e. hashes)
|
||||
pub fn memeq(lhs: &[u8], rhs: &[u8]) -> bool {
|
||||
let size = lhs.len();
|
||||
|
||||
size == rhs.len()
|
||||
&& unsafe {
|
||||
let l = lhs as *const _ as _;
|
||||
let r = rhs as *const _ as _;
|
||||
(l as usize) == (r as usize) || libc::memcmp(l, r, size) == 0
|
||||
}
|
||||
}
|
||||
|
||||
/// Converts the hexstring into a byte vector.
|
||||
///
|
||||
/// Stops if the end or until a non hex chat is found
|
||||
@@ -573,17 +559,4 @@ mod tests {
|
||||
try_parse_u128("00112233445566778899aabbccddeeff", "").unwrap()
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn memeq() {
|
||||
let a = [1, 2, 3, 4, 5, 6, 7, 8, 9, 0];
|
||||
let b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 1];
|
||||
let c = [0, 0, 1, 2, 3, 4];
|
||||
|
||||
assert!(super::memeq(&a, &a));
|
||||
assert!(super::memeq(&a, &a.clone()));
|
||||
assert!(!super::memeq(&b, &a));
|
||||
assert!(!super::memeq(&b, &c));
|
||||
assert!(!super::memeq(&b, &[]));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user