Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[collections] Add a RangeSet collection. #1266

Merged
merged 6 commits into from
Nov 28, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions font-types/src/fixed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ macro_rules! fixed_impl {
Self(self.0.saturating_add(other.0))
}

/// Checked addition.
#[inline(always)]
pub fn checked_add(self, other: Self) -> Option<Self> {
self.0.checked_add(other.0).map(|inner| Self(inner))
}

/// Wrapping substitution.
#[inline(always)]
pub const fn wrapping_sub(self, other: Self) -> Self {
Expand Down
6 changes: 6 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,12 @@ path = "fuzz_targets/fuzz_int_set.rs"
test = false
doc = false

[[bin]]
name = "fuzz_range_set"
path = "fuzz_targets/fuzz_range_set.rs"
test = false
doc = false

[[bin]]
name = "fuzz_ift_patch_group"
path = "fuzz_targets/fuzz_ift_patch_group.rs"
Expand Down
94 changes: 94 additions & 0 deletions fuzz/fuzz_targets/fuzz_range_set.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,94 @@
#![no_main]
//! Fuzzes the incremental_font_transfer patch_group.rs API

use std::ops::RangeInclusive;

use libfuzzer_sys::{arbitrary, fuzz_target};
use read_fonts::collections::{IntSet, RangeSet};

#[derive(Debug, arbitrary::Arbitrary)]
enum Operation {
Insert(RangeInclusive<u16>),
Extend(Vec<RangeInclusive<u16>>),
Intersection,
Iter,
}

#[derive(Debug, arbitrary::Arbitrary)]
enum SetIndex {
One,
Two,
}

#[derive(Default)]
struct State {
range_set: RangeSet<u16>,
int_set: IntSet<u16>,
}

const OP_COUNT_LIMIT: u64 = 1000;

fn range_len(range: &RangeInclusive<u16>) -> u64 {
if range.end() > range.start() {
let count = *range.end() as u64 - *range.start() as u64;
count.saturating_mul(count.ilog2() as u64)
} else {
0
}
}

fuzz_target!(|operations: Vec<(Operation, SetIndex)>| {
let mut state1: State = Default::default();
let mut state2: State = Default::default();
let mut op_count = 0u64;
for (op, index) in operations {
let (state, state_other) = match index {
SetIndex::One => (&mut state1, &mut state2),
SetIndex::Two => (&mut state2, &mut state1),
};

match op {
Operation::Insert(range) => {
op_count = op_count.saturating_add(range_len(&range));
if op_count > OP_COUNT_LIMIT {
return;
}

state.range_set.insert(range.clone());
state.int_set.insert_range(range.clone());
}
Operation::Extend(ranges) => {
for range in ranges.iter() {
op_count = op_count.saturating_add(range_len(range));
if op_count > OP_COUNT_LIMIT {
return;
}
state.int_set.insert_range(range.clone());
}
state.range_set.extend(ranges.into_iter());
}
Operation::Iter => {
op_count = op_count.saturating_add(state.int_set.iter_ranges().count() as u64);
if op_count > OP_COUNT_LIMIT {
return;
}

assert!(state.range_set.iter().eq(state.int_set.iter_ranges()));
}
Operation::Intersection => {
op_count = op_count.saturating_add(state.int_set.len());
op_count = op_count.saturating_add(state_other.int_set.len());
if op_count > OP_COUNT_LIMIT {
return;
}

let mut tmp = state.int_set.clone();
tmp.intersect(&state_other.int_set);
assert!(state
.range_set
.intersection(&state_other.range_set)
.eq(tmp.iter_ranges()));
}
}
}
});
3 changes: 3 additions & 0 deletions read-fonts/src/collections.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,6 @@

pub mod int_set;
pub use int_set::IntSet;

mod range_set;
pub use range_set::RangeSet;
Loading