Skip to content

Commit b32557c

Browse files
authored
Add constructor for UnicodeIdentificationMetadata (#3746)
* Add constructor for UnicodeIdentificationMetadata * Fix formatting
1 parent 0e72b72 commit b32557c

2 files changed

Lines changed: 18 additions & 12 deletions

File tree

crates/libafl/src/mutators/unicode/mod.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use crate::{
1616
mutators::{MutationResult, Mutator, Tokens, rand_range},
1717
nonzero,
1818
stages::{
19-
UnicodeIdentificationMetadata, extract_metadata,
19+
UnicodeIdentificationMetadata,
2020
mutational::{MutatedTransform, MutatedTransformPost},
2121
},
2222
state::{HasCorpus, HasMaxSize, HasRand},
@@ -268,7 +268,7 @@ fn rand_replace_range<S: HasRand + HasMaxSize, F: Fn(&mut S) -> char>(
268268
}
269269

270270
input.0.splice(range, replacement);
271-
input.1 = extract_metadata(input.0.mutator_bytes());
271+
input.1 = UnicodeIdentificationMetadata::new(input.0.mutator_bytes());
272272

273273
MutationResult::Mutated
274274
}
@@ -445,7 +445,7 @@ where
445445
}
446446

447447
input.0.splice(range, token.iter().copied());
448-
input.1 = extract_metadata(input.0.mutator_bytes());
448+
input.1 = UnicodeIdentificationMetadata::new(input.0.mutator_bytes());
449449
return Ok(MutationResult::Mutated);
450450
}
451451

@@ -508,7 +508,7 @@ where
508508
}
509509

510510
input.0.splice(range, token.iter().copied());
511-
input.1 = extract_metadata(input.0.mutator_bytes());
511+
input.1 = UnicodeIdentificationMetadata::new(input.0.mutator_bytes());
512512
return Ok(MutationResult::Mutated);
513513
}
514514

@@ -528,7 +528,7 @@ mod test {
528528
corpus::NopCorpus,
529529
inputs::{BytesInput, HasMutatorBytes},
530530
mutators::{Mutator, UnicodeCategoryRandMutator, UnicodeSubcategoryRandMutator},
531-
stages::extract_metadata,
531+
stages::UnicodeIdentificationMetadata,
532532
state::StdState,
533533
};
534534

@@ -550,7 +550,7 @@ mod test {
550550
)?;
551551

552552
for _ in 0..(1 << 12) {
553-
let metadata = extract_metadata(bytes.mutator_bytes());
553+
let metadata = UnicodeIdentificationMetadata::new(bytes.mutator_bytes());
554554
let mut input = (bytes, metadata);
555555
let _ = mutator.mutate(&mut state, &mut input);
556556
println!(
@@ -585,7 +585,7 @@ mod test {
585585
)?;
586586

587587
for _ in 0..(1 << 12) {
588-
let metadata = extract_metadata(bytes.mutator_bytes());
588+
let metadata = UnicodeIdentificationMetadata::new(bytes.mutator_bytes());
589589
let mut input = (bytes, metadata);
590590
let _ = mutator.mutate(&mut state, &mut input);
591591
println!(

crates/libafl/src/stages/unicode.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,14 +23,22 @@ pub struct UnicodeIdentificationMetadata {
2323
impl_serdeany!(UnicodeIdentificationMetadata);
2424

2525
impl UnicodeIdentificationMetadata {
26+
/// Creates a new [`struct@UnicodeIdentificationMetadata`].
27+
#[must_use]
28+
pub fn new(bytes: &[u8]) -> Self {
29+
Self {
30+
ranges: Rc::new(extract_ranges(bytes)),
31+
}
32+
}
33+
2634
/// The list of pre-computed string-like ranges in the input
2735
#[must_use]
2836
pub fn ranges(&self) -> &Vec<(usize, BitVec)> {
2937
self.ranges.as_ref()
3038
}
3139
}
3240

33-
pub(crate) fn extract_metadata(bytes: &[u8]) -> UnicodeIdentificationMetadata {
41+
fn extract_ranges(bytes: &[u8]) -> Vec<(usize, BitVec)> {
3442
let mut ranges = Vec::new();
3543

3644
if !bytes.is_empty() {
@@ -63,9 +71,7 @@ pub(crate) fn extract_metadata(bytes: &[u8]) -> UnicodeIdentificationMetadata {
6371
}
6472
}
6573

66-
UnicodeIdentificationMetadata {
67-
ranges: Rc::new(ranges),
68-
}
74+
ranges
6975
}
7076

7177
/// Stage which identifies potential strings in the provided input
@@ -101,7 +107,7 @@ impl<I, S> UnicodeIdentificationStage<I, S> {
101107
let input = tc.load_input(state.corpus())?;
102108

103109
let bytes = input.target_bytes();
104-
let metadata = extract_metadata(&bytes);
110+
let metadata = UnicodeIdentificationMetadata::new(&bytes);
105111
tc.add_metadata(metadata);
106112

107113
Ok(())

0 commit comments

Comments
 (0)