Skip to content

Commit 1ae8991

Browse files
committed
misc: listening to clippy
1 parent 3a68b60 commit 1ae8991

File tree

4 files changed

+18
-56
lines changed

4 files changed

+18
-56
lines changed

bmatcher-core/src/compiler/optimizer.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -93,8 +93,7 @@ impl Optimizer {
9393
let is_across_branch = self
9494
.branches
9595
.iter()
96-
.find(|branch| index + 1 == branch.right_index || index + 1 == branch.end_index)
97-
.is_some();
96+
.any(|branch| index + 1 == branch.right_index || index + 1 == branch.end_index);
9897
if is_across_branch {
9998
index += 1;
10099
continue;

bmatcher-core/src/matcher.rs

Lines changed: 13 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use alloc::vec;
12
use alloc::vec::Vec;
23

34
use crate::{Atom, BinaryPattern, JumpType, MatchTarget, ReadWidth};
@@ -17,18 +18,12 @@ pub struct BinaryMatcher<'a> {
1718

1819
impl<'a> BinaryMatcher<'a> {
1920
pub fn new(pattern: &'a dyn BinaryPattern, target: &'a dyn MatchTarget) -> Self {
20-
let mut save_stack = Vec::new();
21-
save_stack.resize(8, 0);
22-
23-
let mut cursor_stack = Vec::new();
24-
cursor_stack.resize(8, 0);
25-
2621
Self {
2722
pattern,
2823
target,
2924

30-
save_stack,
31-
cursor_stack,
25+
save_stack: vec![0; 8],
26+
cursor_stack: vec![0; 8],
3227

3328
match_offset: 0,
3429
}
@@ -42,11 +37,7 @@ impl<'a> BinaryMatcher<'a> {
4237
match atoms[atom_cursor] {
4338
Atom::ByteSequence { seq_start, seq_end } => {
4439
let expected_bytes = &self.pattern.byte_sequence()[seq_start..seq_end];
45-
let Some(actual_bytes) =
46-
self.target.subrange(data_cursor, expected_bytes.len())
47-
else {
48-
return None;
49-
};
40+
let actual_bytes = self.target.subrange(data_cursor, expected_bytes.len())?;
5041

5142
if expected_bytes != actual_bytes {
5243
return None;
@@ -104,16 +95,11 @@ impl<'a> BinaryMatcher<'a> {
10495
self.save_stack.truncate(save_stack_size);
10596
self.cursor_stack.truncate(cursor_stack_size);
10697

107-
if let Some(data_cursor) = self.match_atoms(
98+
self.match_atoms(
10899
data_cursor,
109100
&atoms[atom_cursor + 1 + left_len
110101
..atom_cursor + 1 + left_len + right_len],
111-
) {
112-
data_cursor
113-
} else {
114-
/* non of the branches matched */
115-
return None;
116-
}
102+
)?
117103
};
118104

119105
atom_cursor += 1 + left_len + right_len;
@@ -122,30 +108,18 @@ impl<'a> BinaryMatcher<'a> {
122108
Atom::Jump(mode) => {
123109
data_cursor = match mode {
124110
JumpType::RelByte => {
125-
let Some(value) = self.target.subrange(data_cursor, 1) else {
126-
return None;
127-
};
128-
111+
let value = self.target.subrange(data_cursor, 1)?;
129112
(data_cursor + 1).wrapping_add_signed(value[0] as i8 as isize)
130113
}
131114
JumpType::RelDWord => {
132-
let Some(value) = self.target.subrange(data_cursor, 4) else {
133-
return None;
134-
};
135-
115+
let value = self.target.subrange(data_cursor, 4)?;
136116
let value = i32::from_le_bytes(value.try_into().unwrap());
137117
(data_cursor + 4).wrapping_add_signed(value as isize)
138118
}
139119
JumpType::AbsQWord => {
140-
let Some(value) = self.target.subrange(data_cursor, 8) else {
141-
return None;
142-
};
120+
let value = self.target.subrange(data_cursor, 8)?;
143121
let value = u64::from_le_bytes(value.try_into().unwrap());
144-
let Some(value) = self.target.translate_absolute_address(value) else {
145-
return None;
146-
};
147-
148-
value
122+
self.target.translate_absolute_address(value)?
149123
}
150124
};
151125
atom_cursor += 1;
@@ -154,24 +128,15 @@ impl<'a> BinaryMatcher<'a> {
154128
Atom::Read(width) => {
155129
let (value, width) = match width {
156130
ReadWidth::Byte => {
157-
let Some(value) = self.target.subrange(data_cursor, 1) else {
158-
return None;
159-
};
160-
131+
let value = self.target.subrange(data_cursor, 1)?;
161132
(value[0] as u32, 1)
162133
}
163134
ReadWidth::Word => {
164-
let Some(value) = self.target.subrange(data_cursor, 2) else {
165-
return None;
166-
};
167-
135+
let value = self.target.subrange(data_cursor, 2)?;
168136
(u16::from_le_bytes(value.try_into().unwrap()) as u32, 2)
169137
}
170138
ReadWidth::DWord => {
171-
let Some(value) = self.target.subrange(data_cursor, 4) else {
172-
return None;
173-
};
174-
139+
let value = self.target.subrange(data_cursor, 4)?;
175140
(u32::from_le_bytes(value.try_into().unwrap()), 4)
176141
}
177142
};

bmatcher-core/src/pattern.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -68,13 +68,13 @@ impl<'a> BorrowedBinaryPattern<'a> {
6868
}
6969
}
7070

71-
impl<'a> BinaryPattern for BorrowedBinaryPattern<'a> {
71+
impl BinaryPattern for BorrowedBinaryPattern<'_> {
7272
fn atoms(&self) -> &[Atom] {
73-
&self.atoms
73+
self.atoms
7474
}
7575

7676
fn byte_sequence(&self) -> &[u8] {
77-
&self.byte_sequence
77+
self.byte_sequence
7878
}
7979
}
8080

bmatcher-core/src/target.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -44,9 +44,7 @@ impl MatchTarget for &[u8] {
4444

4545
fn translate_absolute_address(&self, address: u64) -> Option<usize> {
4646
let own_address = self.as_ptr() as u64;
47-
if own_address < address {
48-
None
49-
} else if address >= own_address + self.len() as u64 {
47+
if own_address < address || address >= own_address + self.len() as u64 {
5048
None
5149
} else {
5250
Some((address - own_address) as usize)

0 commit comments

Comments
 (0)