Skip to content

Commit 2bbf4ef

Browse files
committed
Uh-oh
1 parent 1371646 commit 2bbf4ef

File tree

1 file changed

+21
-46
lines changed

1 file changed

+21
-46
lines changed

libafl/src/inputs/mod.rs

+21-46
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ pub mod generalized;
1313
pub use generalized::*;
1414

1515
pub mod bytessub;
16-
pub use bytessub::{BytesSlice, BytesSubInput};
16+
pub use bytessub::BytesSubInput;
1717

1818
#[cfg(feature = "multipart_inputs")]
1919
pub mod multi;
@@ -36,13 +36,14 @@ use std::{fs::File, hash::Hash, io::Read, path::Path};
3636
use libafl_bolts::fs::write_file_atomic;
3737
use libafl_bolts::{
3838
ownedref::{OwnedMutSlice, OwnedSlice},
39+
subrange::{PartialSubRangeSlice, SubRangeSlice, SubRangeSliceMut},
3940
Error, HasLen,
4041
};
4142
#[cfg(feature = "nautilus")]
4243
pub use nautilus::*;
4344
use serde::{Deserialize, Serialize};
4445

45-
use crate::{corpus::CorpusId, inputs::bytessub::BytesSliceMut};
46+
use crate::corpus::CorpusId;
4647

4748
/// An input for the target
4849
#[cfg(not(feature = "std"))]
@@ -140,35 +141,6 @@ pub struct BytesReader<'a> {
140141
pos: usize,
141142
}
142143

143-
/// Representation of a partial slice
144-
/// This is used when providing a slice smaller than the expected one.
145-
/// It notably happens when trying to read the end of an input.
146-
#[derive(Debug)]
147-
pub enum PartialBytesSubInput<'a> {
148-
/// The slice is empty, and thus not kept
149-
Empty,
150-
/// The slice is strictly smaller than the expected one.
151-
Partial(BytesSlice<'a>),
152-
}
153-
154-
impl<'a> PartialBytesSubInput<'a> {
155-
/// Consumes `PartialBytesSubInput` and returns true if it was empty, false otherwise.
156-
#[must_use]
157-
pub fn empty(self) -> bool {
158-
matches!(self, PartialBytesSubInput::Empty)
159-
}
160-
161-
/// Consumes `PartialBytesSubInput` and returns the partial slice if it was a partial slice, None otherwise.
162-
#[must_use]
163-
pub fn partial(self) -> Option<BytesSlice<'a>> {
164-
#[allow(clippy::match_wildcard_for_single_variants)]
165-
match self {
166-
PartialBytesSubInput::Partial(partial_slice) => Some(partial_slice),
167-
_ => None,
168-
}
169-
}
170-
}
171-
172144
impl<'a> BytesReader<'a> {
173145
/// Create a new [`BytesReader`].
174146
/// The position of the reader is initialized to 0.
@@ -184,8 +156,8 @@ impl<'a> BytesReader<'a> {
184156
/// If the resulting slice would go beyond the end of the parent input, it will be truncated to the length of the parent input.
185157
/// This function does not provide any feedback on whether the slice was cropped or not.
186158
#[must_use]
187-
pub fn next_sub_slice_truncated(&mut self, limit: usize) -> BytesSlice<'a> {
188-
let sub_input = BytesSlice::with_slice(self.parent_input, self.pos..(self.pos + limit));
159+
pub fn next_sub_slice_truncated(&mut self, limit: usize) -> PartialSubRangeSlice<'a, u8> {
160+
let sub_input = SubRangeSlice::with_slice(self.parent_input, self.pos..(self.pos + limit));
189161

190162
self.pos += sub_input.len();
191163

@@ -201,15 +173,15 @@ impl<'a> BytesReader<'a> {
201173
pub fn next_sub_input(
202174
&mut self,
203175
limit: usize,
204-
) -> Result<BytesSlice<'a>, PartialBytesSubInput<'a>> {
176+
) -> Result<SubRangeSlice<'a, u8>, PartialSubRangeSlice<'a, u8>> {
205177
let slice_to_return = self.next_sub_slice_truncated(limit);
206178

207179
let real_len = slice_to_return.len();
208180

209181
if real_len == 0 {
210-
Err(PartialBytesSubInput::Empty)
182+
Err(PartialSubRangeSlice::Empty)
211183
} else if real_len < limit {
212-
Err(PartialBytesSubInput::Partial(slice_to_return))
184+
Err(PartialSubRangeSlice::Partial(slice_to_return))
213185
} else {
214186
Ok(slice_to_return)
215187
}
@@ -260,15 +232,15 @@ pub trait HasMutatorBytes: HasLen {
260232
R: RangeBounds<usize>;
261233

262234
/// Creates a [`BytesSlice`] from this input, that can be used to slice a byte array.
263-
fn sub_bytes<R>(&self, range: R) -> BytesSlice
235+
fn sub_bytes<R>(&self, range: R) -> SubRangeSlice<'a, u8>
264236
where
265237
R: RangeBounds<usize>,
266238
{
267-
BytesSlice::new(OwnedSlice::from(self.bytes()), range)
239+
SubRangeSlice::new(OwnedSlice::from(self.bytes()), range)
268240
}
269241

270242
/// Creates a [`BytesSliceMut`] from this input, that can be used to slice a byte array.
271-
fn sub_bytes_mut<R>(&mut self, range: R) -> BytesSliceMut
243+
fn sub_bytes_mut<R>(&mut self, range: R) -> SubRangeSliceMut<'a, u8>
272244
where
273245
R: RangeBounds<usize>,
274246
{
@@ -421,17 +393,17 @@ mod tests {
421393
let mut bytes_reader = BytesReader::new(&bytes_input);
422394

423395
let bytes_read = bytes_reader.next_sub_slice_truncated(2);
424-
assert_eq!(*bytes_read.bytes(), [1, 2]);
396+
assert_eq!(*bytes_read.as_slice(), [1, 2]);
425397

426398
let bytes_read = bytes_reader.next_sub_slice_truncated(3);
427-
assert_eq!(*bytes_read.bytes(), [3, 4, 5]);
399+
assert_eq!(*bytes_read.as_slice(), [3, 4, 5]);
428400

429401
let bytes_read = bytes_reader.next_sub_slice_truncated(8);
430-
assert_eq!(*bytes_read.bytes(), [6, 7]);
402+
assert_eq!(*bytes_read.as_slice(), [6, 7]);
431403

432404
let bytes_read = bytes_reader.next_sub_slice_truncated(8);
433405
let bytes_read_ref: &[u8] = &[];
434-
assert_eq!(&*bytes_read.bytes(), bytes_read_ref);
406+
assert_eq!(&*bytes_read.as_slice(), bytes_read_ref);
435407
}
436408

437409
#[test]
@@ -440,13 +412,16 @@ mod tests {
440412
let mut bytes_reader = BytesReader::new(&bytes_input);
441413

442414
let bytes_read = bytes_reader.next_sub_input(2);
443-
assert_eq!(*bytes_read.unwrap().bytes(), [1, 2]);
415+
assert_eq!(*bytes_read.unwrap().as_slice(), [1, 2]);
444416

445417
let bytes_read = bytes_reader.next_sub_input(3);
446-
assert_eq!(*bytes_read.unwrap().bytes(), [3, 4, 5]);
418+
assert_eq!(*bytes_read.unwrap().as_slice(), [3, 4, 5]);
447419

448420
let bytes_read = bytes_reader.next_sub_input(8);
449-
assert_eq!(*bytes_read.unwrap_err().partial().unwrap().bytes(), [6, 7]);
421+
assert_eq!(
422+
*bytes_read.unwrap_err().partial().unwrap().as_slice(),
423+
[6, 7]
424+
);
450425

451426
let bytes_read = bytes_reader.next_sub_input(8);
452427
assert!(bytes_read.unwrap_err().empty());

0 commit comments

Comments
 (0)