Skip to content

Commit

Permalink
Introduce fuzz tests for wrap_optimal_fit and wrap_first_fit
Browse files Browse the repository at this point in the history
These fuzz tests immediately found the problem reported in #247 and
gave a short way of reproducing it:

    wrap("x y", 515566821223)

will currently panic due to an overflow error. The wrap_first_fit
function seems to not crash.

Run the fuzz tests with:

    $ cargo fuzz run fill_optimal_fit -- -only_ascii=1

You’ll need to `cargo install cargo-fuzz` first.
  • Loading branch information
mgeisler committed Dec 18, 2020
1 parent 167d73e commit 256a668
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 1 deletion.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ keywords = ["text", "formatting", "wrap", "typesetting", "hyphenation"]
categories = ["text-processing", "command-line-interface"]
license = "MIT"
edition = "2018"
exclude = [".github/", ".gitignore", "benches/", "examples/"]
exclude = [".github/", ".gitignore", "benches/", "examples/", "fuzz/"]

[package.metadata.docs.rs]
all-features = true
Expand Down
3 changes: 3 additions & 0 deletions fuzz/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
artifacts/
corpus/
target/
30 changes: 30 additions & 0 deletions fuzz/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@

[package]
name = "textwrap-fuzz"
version = "0.0.0"
authors = ["Automatically generated"]
publish = false
edition = "2018"

[package.metadata]
cargo-fuzz = true

[dependencies]
libfuzzer-sys = "0.3"
textwrap = { path = ".." }

# Prevent this from interfering with workspaces
[workspace]
members = ["."]

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

[[bin]]
name = "fill_first_fit"
path = "fuzz_targets/fill_first_fit.rs"
test = false
doc = false
9 changes: 9 additions & 0 deletions fuzz/fuzz_targets/fill_first_fit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![no_main]
use libfuzzer_sys::fuzz_target;
use textwrap::core::WrapAlgorithm::FirstFit;
use textwrap::Options;

fuzz_target!(|input: (String, usize)| {
let options = Options::new(input.1).wrap_algorithm(FirstFit);
let _ = textwrap::fill(&input.0, &options);
});
9 changes: 9 additions & 0 deletions fuzz/fuzz_targets/fill_optimal_fit.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#![no_main]
use libfuzzer_sys::fuzz_target;
use textwrap::core::WrapAlgorithm::OptimalFit;
use textwrap::Options;

fuzz_target!(|input: (String, usize)| {
let options = Options::new(input.1).wrap_algorithm(OptimalFit);
let _ = textwrap::fill(&input.0, &options);
});

0 comments on commit 256a668

Please sign in to comment.