Skip to content

Commit

Permalink
run clippy --fix
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrmaxmeier committed May 8, 2024
1 parent 33e7bea commit 0d04650
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 96 deletions.
8 changes: 4 additions & 4 deletions lain/src/buffer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ where
let size = self
.iter()
.map(SerializedSize::serialized_size)
.fold(0, |sum, i| sum + i);
.sum::<usize>();

size
}
Expand Down Expand Up @@ -167,23 +167,23 @@ impl BinarySerialize for bool {
impl BinarySerialize for i8 {
#[inline(always)]
fn binary_serialize<W: Write, E: ByteOrder>(&self, buffer: &mut W) -> usize {
buffer.write_i8(*self as i8).unwrap();
buffer.write_i8(*self).unwrap();
std::mem::size_of::<i8>()
}
}

impl BinarySerialize for u8 {
#[inline(always)]
fn binary_serialize<W: Write, E: ByteOrder>(&self, buffer: &mut W) -> usize {
buffer.write_u8(*self as u8).unwrap();
buffer.write_u8(*self).unwrap();
std::mem::size_of::<u8>()
}
}

impl BinarySerialize for [u8] {
#[inline(always)]
fn binary_serialize<W: Write, E: ByteOrder>(&self, buffer: &mut W) -> usize {
buffer.write(&self).unwrap()
buffer.write(self).unwrap()
}
}

Expand Down
12 changes: 6 additions & 6 deletions lain/src/dangerous_numbers.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::rand::Rng;
use crate::traits::*;

static DANGEROUS_NUMBERS_U8: &'static [u8] = &[
static DANGEROUS_NUMBERS_U8: &[u8] = &[
std::u8::MIN, // 0x00
std::u8::MAX, // 0xff
std::i8::MAX as u8, // 0x7f
(std::i8::MAX as u8) + 1, // 0x80
];

static DANGEROUS_NUMBERS_U16: &'static [u16] = &[
static DANGEROUS_NUMBERS_U16: &[u16] = &[
// big-endian variants
std::u16::MIN, // 0x0000
std::u16::MAX, // 0xffff
Expand All @@ -19,7 +19,7 @@ static DANGEROUS_NUMBERS_U16: &'static [u16] = &[
0x0080,
];

static DANGEROUS_NUMBERS_U32: &'static [u32] = &[
static DANGEROUS_NUMBERS_U32: &[u32] = &[
// big-endian variants
std::u32::MIN,
std::u32::MAX,
Expand All @@ -30,7 +30,7 @@ static DANGEROUS_NUMBERS_U32: &'static [u32] = &[
0x0000_0080,
];

static DANGEROUS_NUMBERS_U64: &'static [u64] = &[
static DANGEROUS_NUMBERS_U64: &[u64] = &[
// big-endian variants
std::u64::MIN,
std::u64::MAX,
Expand All @@ -41,7 +41,7 @@ static DANGEROUS_NUMBERS_U64: &'static [u64] = &[
0x0000_0000_0000_0080,
];

static DANGEROUS_NUMBERS_F32: &'static [f32] = &[
static DANGEROUS_NUMBERS_F32: &[f32] = &[
std::f32::INFINITY,
std::f32::MAX,
std::f32::MIN,
Expand All @@ -50,7 +50,7 @@ static DANGEROUS_NUMBERS_F32: &'static [f32] = &[
std::f32::NEG_INFINITY,
];

static DANGEROUS_NUMBERS_F64: &'static [f64] = &[
static DANGEROUS_NUMBERS_F64: &[f64] = &[
std::f64::INFINITY,
std::f64::MAX,
std::f64::MIN,
Expand Down
5 changes: 2 additions & 3 deletions lain/src/driver.rs
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ impl<T: 'static + Send + Sync> FuzzerDriver<T> {
}

pub fn global_context(&self) -> Option<Arc<RwLock<T>>> {
self.global_context.as_ref().map(|c| Arc::clone(c))
self.global_context.as_ref().map(Arc::clone)
}

/// Sets the root seed
Expand Down Expand Up @@ -243,8 +243,7 @@ pub fn start_fuzzer<F: 'static, C: 'static, T: 'static + Send + Sync>(

mutator.random_flags();

if let Err(_) =
(callback)(&mut mutator, &mut context, thread_driver.global_context())
if (callback)(&mut mutator, &mut context, thread_driver.global_context()).is_err()
{
thread_driver
.num_failed_iterations
Expand Down
6 changes: 3 additions & 3 deletions lain/src/mutatable.rs
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ fn grow_vec<T: NewFuzzed + SerializedSize, R: Rng>(
};

// If we were given a size constraint, we need to respect it
if let Some(max_size) = max_size.clone() {
if let Some(max_size) = max_size {
num_elements = min(num_elements, max_size / T::max_default_object_size());
}

Expand Down Expand Up @@ -327,7 +327,7 @@ where
});

// Check if we can even mutate this item
if let Some(max_size) = constraints.as_ref().map(|c| c.max_size).flatten().clone() {
if let Some(max_size) = constraints.as_ref().and_then(|c| c.max_size) {
if T::min_nonzero_elements_size() < max_size || T::max_default_object_size() > max_size
{
return;
Expand Down Expand Up @@ -367,7 +367,7 @@ where
});

// Check if we can even mutate this item
if let Some(max_size) = constraints.as_ref().map(|c| c.max_size).flatten().clone() {
if let Some(max_size) = constraints.as_ref().and_then(|c| c.max_size) {
if T::min_nonzero_elements_size() < max_size {
return;
}
Expand Down
15 changes: 5 additions & 10 deletions lain/src/mutator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,15 +38,11 @@ struct MutatorFlags {
/// Represents the state of the current corpus item being fuzzed.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serde_support", derive(Serialize, Deserialize))]
#[derive(Default)]
pub struct CorpusFuzzingState {
fields_fuzzed: usize,
}

impl Default for CorpusFuzzingState {
fn default() -> Self {
CorpusFuzzingState { fields_fuzzed: 0 }
}
}

impl CorpusFuzzingState {
pub fn reset(&mut self) {
Expand Down Expand Up @@ -104,7 +100,7 @@ impl<R: Rng> Mutator<R> {
+ std::fmt::Debug,
{
// dirty but needs to be done so we can call self.gen_chance_ignore_flags
if let Some(count) = self.flags.field_count.clone() {
if let Some(count) = self.flags.field_count {
if self.corpus_state.fields_fuzzed == count {
return;
}
Expand Down Expand Up @@ -258,9 +254,9 @@ impl<R: Rng> Mutator<R> {
let zero = B1::from(0u8).unwrap();

let mut slices = [
((zero.clone(), zero.clone()), 0u8),
((zero.clone(), zero.clone()), 0u8),
((zero.clone(), zero.clone()), 0u8),
((zero, zero), 0u8),
((zero, zero), 0u8),
((zero, zero), 0u8),
];

for i in 0..3 {
Expand Down Expand Up @@ -307,7 +303,6 @@ impl<R: Rng> Mutator<R> {
pub fn should_early_bail_mutation(&self) -> bool {
self.flags
.field_count
.clone()
.map(|count| count >= self.corpus_state.fields_fuzzed)
.unwrap_or(false)
}
Expand Down
14 changes: 7 additions & 7 deletions lain/src/new_fuzzed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ where

// if no min/max were supplied, we'll take a conservative approach of 64 elements
match constraints {
Some(ref constraints) => {
Some(constraints) => {
min = constraints.min.unwrap_or(0);
max = constraints.max.unwrap_or(MAX_NUM_ELEMENTS);

Expand Down Expand Up @@ -127,7 +127,7 @@ where
T::new_fuzzed(
mutator,
Some(
&Constraints::new()
Constraints::new()
.max_size(max_size - used_size)
.set_base_size_accounted_for(),
),
Expand Down Expand Up @@ -239,7 +239,7 @@ where
T::new_fuzzed(
mutator,
Some(
&Constraints::new()
Constraints::new()
.max_size(max_size - used_size)
.set_base_size_accounted_for(),
),
Expand Down Expand Up @@ -267,7 +267,7 @@ where
T::new_fuzzed(
mutator,
Some(
&Constraints::new()
Constraints::new()
.max_size(max_size - used_size)
.set_base_size_accounted_for(),
),
Expand Down Expand Up @@ -380,7 +380,7 @@ impl NewFuzzed for Utf8String {

// if no min/max were supplied, we'll take a conservative approach
match constraints {
Some(ref constraints) => {
Some(constraints) => {
min = constraints.min.unwrap_or(0);
max = constraints.max.unwrap_or(256);
weight = constraints.weighted;
Expand Down Expand Up @@ -444,7 +444,7 @@ impl NewFuzzed for AsciiString {

// if no min/max were supplied, we'll take a conservative approach
match constraints {
Some(ref constraints) => {
Some(constraints) => {
min = constraints.min.unwrap_or(0);
max = constraints.max.unwrap_or(256);
weight = constraints.weighted;
Expand Down Expand Up @@ -617,7 +617,7 @@ impl NewFuzzed for AsciiChar {

// if no min/max were supplied, we'll take a conservative approach of 64 elements
match constraints {
Some(ref constraints) => {
Some(constraints) => {
min = constraints.min.unwrap_or(0);
max = constraints.max.unwrap_or(0x80);
weight = constraints.weighted;
Expand Down
27 changes: 12 additions & 15 deletions lain/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ pub struct Utf8String {
impl Utf8String {
pub fn new(s: &str) -> Self {
Utf8String {
inner: s.chars().map(|c| Utf8Char(c)).collect(),
inner: s.chars().map(Utf8Char).collect(),
}
}
}
Expand All @@ -95,7 +95,7 @@ pub struct AsciiString {
impl AsciiString {
pub fn new(s: &str) -> Self {
AsciiString {
inner: s.chars().map(|c| AsciiChar(c)).collect(),
inner: s.chars().map(AsciiChar).collect(),
}
}
}
Expand Down Expand Up @@ -134,29 +134,29 @@ impl<T: Bounded + Debug> Constraints<T> {
}
}

pub fn min<'a>(&'a mut self, min: T) -> &'a mut Constraints<T> {
pub fn min(&mut self, min: T) -> &mut Constraints<T> {
self.min = Some(min);
self
}

pub fn max<'a>(&'a mut self, max: T) -> &'a mut Constraints<T> {
pub fn max(&mut self, max: T) -> &mut Constraints<T> {
self.max = Some(max);
self
}

pub fn weighted<'a>(&'a mut self, weighted: Weighted) -> &'a mut Constraints<T> {
pub fn weighted(&mut self, weighted: Weighted) -> &mut Constraints<T> {
self.weighted = weighted;
self
}

pub fn max_size<'a>(&'a mut self, max_size: usize) -> &'a mut Constraints<T> {
pub fn max_size(&mut self, max_size: usize) -> &mut Constraints<T> {
self.max_size = Some(max_size);
self
}

pub fn account_for_base_object_size<'a, U: crate::traits::SerializedSize>(
&'a mut self,
) -> &'a mut Constraints<T> {
pub fn account_for_base_object_size<U: crate::traits::SerializedSize>(
&mut self,
) -> &mut Constraints<T> {
if !self.base_object_size_accounted_for {
if let Some(ref mut max_size) = self.max_size {
if U::max_default_object_size() > *max_size {
Expand All @@ -172,22 +172,19 @@ impl<T: Bounded + Debug> Constraints<T> {
self
}

pub fn set_base_size_accounted_for<'a>(&'a mut self) -> &'a mut Constraints<T> {
pub fn set_base_size_accounted_for(&mut self) -> &mut Constraints<T> {
self.base_object_size_accounted_for = true;
self
}
}

/// Which direction to weigh ranges towards (min bound, upper bound, or none).
#[derive(Debug, PartialEq, Clone, Copy)]
#[derive(Default)]
pub enum Weighted {
#[default]
None,
Min,
Max,
}

impl Default for Weighted {
fn default() -> Self {
Weighted::None
}
}
8 changes: 4 additions & 4 deletions lain_derive/src/internals/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn fields_from_ast<'a>(cx: &Ctxt, fields: &'a Punctuated<syn::Field, Token![,]>)

let bits_in_type: usize;

let bitfield_type = field.attrs.bitfield_type().unwrap_or(&field.ty);
let bitfield_type = field.attrs.bitfield_type().unwrap_or(field.ty);
if is_primitive_type(bitfield_type, "u8") {
bits_in_type = 8
} else if is_primitive_type(bitfield_type, "u16") {
Expand All @@ -148,22 +148,22 @@ fn fields_from_ast<'a>(cx: &Ctxt, fields: &'a Punctuated<syn::Field, Token![,]>)
} else if is_primitive_type(bitfield_type, "u64") {
bits_in_type = 64
} else {
cx.error_spanned_by(&field.ty, "Unsupported bitfield datatype. Did you forget to specify `#[lain(bitfield_type = \"...\")]`?");
cx.error_spanned_by(field.ty, "Unsupported bitfield datatype. Did you forget to specify `#[lain(bitfield_type = \"...\")]`?");
return field;
}

if bitfield_bits == bits_in_type {
bitfield_bits = 0;
} else if bitfield_bits > bits_in_type {
cx.error_spanned_by(&field.ty, "Number of bits specified overflows bitfield type");
cx.error_spanned_by(field.ty, "Number of bits specified overflows bitfield type");
}
}

field
})
.collect();

if fields.len() == 0 {
if fields.is_empty() {
return fields;
}

Expand Down
Loading

0 comments on commit 0d04650

Please sign in to comment.