From 256a668f47df9f1dc0f0f05b8eef4021ce7c81cb Mon Sep 17 00:00:00 2001 From: Martin Geisler Date: Fri, 18 Dec 2020 08:00:02 +0100 Subject: [PATCH] Introduce fuzz tests for wrap_optimal_fit and wrap_first_fit MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 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. --- Cargo.toml | 2 +- fuzz/.gitignore | 3 +++ fuzz/Cargo.toml | 30 +++++++++++++++++++++++++++ fuzz/fuzz_targets/fill_first_fit.rs | 9 ++++++++ fuzz/fuzz_targets/fill_optimal_fit.rs | 9 ++++++++ 5 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 fuzz/.gitignore create mode 100644 fuzz/Cargo.toml create mode 100644 fuzz/fuzz_targets/fill_first_fit.rs create mode 100644 fuzz/fuzz_targets/fill_optimal_fit.rs diff --git a/Cargo.toml b/Cargo.toml index 71163cab..9117c774 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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 diff --git a/fuzz/.gitignore b/fuzz/.gitignore new file mode 100644 index 00000000..188f1960 --- /dev/null +++ b/fuzz/.gitignore @@ -0,0 +1,3 @@ +artifacts/ +corpus/ +target/ diff --git a/fuzz/Cargo.toml b/fuzz/Cargo.toml new file mode 100644 index 00000000..193b1f4d --- /dev/null +++ b/fuzz/Cargo.toml @@ -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 diff --git a/fuzz/fuzz_targets/fill_first_fit.rs b/fuzz/fuzz_targets/fill_first_fit.rs new file mode 100644 index 00000000..3e1f0a7f --- /dev/null +++ b/fuzz/fuzz_targets/fill_first_fit.rs @@ -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); +}); diff --git a/fuzz/fuzz_targets/fill_optimal_fit.rs b/fuzz/fuzz_targets/fill_optimal_fit.rs new file mode 100644 index 00000000..13740cb7 --- /dev/null +++ b/fuzz/fuzz_targets/fill_optimal_fit.rs @@ -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); +});